C++ cast raw table to another table type -


i want write class similar std::array c++11. declaring table of type char inside class , later call placement new on table after use table if regular table of type t , here comes trouble.

generally variable like:

char tab[size];  

is of type char(&)[size] , if that's use reinterpret_cast on table cast table of type, in fact using, more or less code this:

char tab[sizeof(t)*size]; t tabt[size] = reinterpret_cast<t(&)[size]>(tab); // more code using tabt 

however in context tab seen char* type. reason, why thought work ability write following template function

template <typename t, size_t size> function(t(&table)[size]){     //do stuff connected table of type t , size size. } 

i know without fancy magic here, want know, why not work.

so question is: there way thing want , there more elegant way mentioned job?

ps: not declare raw table of type t : t tab[size], because wouldn't able create elements, there no constructor without arguments.

the cast doesn't you:

char tab[sizeof(t)*size]; t tabt[size] = reinterpret_cast<t(&)[size]>(tab); 

since arrays aren't copyable code doesn't compile when instantiated. @ least you'd need use

t (&tabt)[size] = reinterpret_cast<t(&)[size]>(tab); 

however, i'd recommend not store uninitialized elements char array start with. use union nested array:

template <typename t, int size> class array {     union data {         data() {}         t values[size];     } data; public:     array() { /* initialize elements appropriately */ }     ~array() { /* destroy initialized elements */ }     // ... }; 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -