B
Bastien Chevreux
Dear all,
I want to write a function that returns the size of an element of STL
containers of any type (vector, list, etc.).
What I've come up is this:
------------------ snip -----------------------
template<class myType>
size_t sizeOfElem(myType & thecontainer)
{
return sizeof(thecontainer.front());
}
------------------ snap -----------------------
I can now use that function on any STL container that supports front:
------------------ snip -----------------------
{
vector<int32_t> v;
cout << sizeOfElem(v) << endl;
}
------------------ snap -----------------------
It works, even with empty containers (at least it does not crash on me and
gives back the correct size). But I am worried about using front() when the
container is empty.
Now my questions: could this have side effects? Are there any better ways to
do that?
Regards,
Bastien
I want to write a function that returns the size of an element of STL
containers of any type (vector, list, etc.).
What I've come up is this:
------------------ snip -----------------------
template<class myType>
size_t sizeOfElem(myType & thecontainer)
{
return sizeof(thecontainer.front());
}
------------------ snap -----------------------
I can now use that function on any STL container that supports front:
------------------ snip -----------------------
{
vector<int32_t> v;
cout << sizeOfElem(v) << endl;
}
------------------ snap -----------------------
It works, even with empty containers (at least it does not crash on me and
gives back the correct size). But I am worried about using front() when the
container is empty.
Now my questions: could this have side effects? Are there any better ways to
do that?
Regards,
Bastien