c++ - boost::any maps with char[] -



c++ - boost::any maps with char[] -

in class, have char data[1000]. want able have map have map of key type string mapped type of info want.

in driver, want able out values map. using map able hold info class not restricted info defined in header class can add together , remove info @ runtime.

for illustration class definition:

public: void populatemap() { classmap["imgdata"] = data; }; private: map<string,boost::any> classmap; char data[1000];

driver:

int main(){ myclass test; map<string,boost::any> mymap; test.populatemap()//populates map char info mymap = test.getmap(); char mydata[1000] = boost::any_cast<char*>(mymap["imgdata"]); //runtime error }

when this, runtime casting errors. i'm not versed in char[definite_size] versus char *. can direct me in problem is? have not seen many examples of people storing char array's in maps. proper way store char[definite_size] map? casting out of map (boost::any requires cast proper type when pulling out of map)

in c++ , in c, there difference between array types , pointer types, though 2 closely related. example, in c , c++, type of

int array[137];

is int[137], not int*. said, when utilize array in c, decays pointer first element. in c++, however, possible reference array. example, code legal:

/* function takes reference array of 137 elements parameter! */ void myfunction(int (&array)[137]) { /* ... */ } int myarray[137]; myfunction(myarray);

here, type of argument myfunction int (&)[137], reference array of 137 integers.

this comes when using templates. example, if have template function:

template <typename t> void myfunction(t& arg) { /* ... */ } int myarray[137]; myfunction(myarray);

then phone call myfunction fill in arg int[137], rather int*, because argument has type int[137].

the reason problem (iirc) boost::any type's assignment operator template looks @ type of argument , stores information. result, if say

boost::any myany; int myarray[137]; myany = myarray;

i think have myany variable store object of type int[137] rather int*, because pointer type doesn't decay.

to prepare this, should consider using new std::array type explicitly indicate have array type beingness stored. way, wouldn't inquire pointer, instead seek pull out std::array<int, 137>. alternatively, if know fact you're using char[1000] stored type, may able reengineer programme don't utilize boost::any here. consider using typedef export default type beingness stored (here, char[1000]) cast proper type.

hope helps!

c++ arrays boost casting map

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -