I want to write class similar to the std::array
from C++11. To do this I am declaring a table of type char
inside this class and later I would like to call placement new
on this table After that I would like to use table as if it was regular table of type T
and here comes the trouble.
Generally variable like:
char tab[size];
Is of type char(&)[size]
and if that's so I would like to use reinterpret_cast
on this table to cast it to table of type, that I am in fact using, so more or less I would like my code to look like this:
char tab[sizeof(T)*size]; T tabT[size] = reinterpret_cast<T(&)[size]>(tab); // more code using tabT
However in this context tab is seen as char*
type. Reason, why I thought it could work is ability to write following template function
template <typename T, size_t size> function(T(&table)[size]){ //do stuff connected with table of type T and size size. }
I know I could do this without this fancy magic here, but I just want to know, why it does not work.
So my question is: Is there a way to do the thing I want to do and is there any more elegant way to do mentioned job?
PS: I do not declare raw table of type T like : T tab[size]
, because I wouldn't be able to create elements, where there is no constructor without arguments.