while doing some TMP 🙂 🙁
I ended up needing the size of the element that STL container holds.
I know that usually deref an container.begin() iterator is bad without checking if container isnt empty, but from what I know sizeof is compiletime macro so I guess it is safe.
Am I right?
code snippet is :
for(auto it = t.begin(); it!= t.end(); ++it)
{
char* cp = (char*)(&(*it));
for (size_t i =0 ; i < sizeof *t.begin();++i)
//...
}
Yes, that’s safe, but realize that you can get at the types stored in a container via the
key_type,mapped_typeand/orvalue_typetypedefmembers.E.g.:
[
sizeofis not a macro, btw. It’s an operator built into the language itself, like++or|=.]