Using "sizeof()" on first element of empty STL container?

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
 
J

Juha Nieminen

Bastien said:
I want to write a function that returns the size of an element of STL
containers of any type (vector, list, etc.).

The standard way to get the value type of an STL container is
container::value_type. Thus you can do this:

template<class myType>
size_t sizeOfElem(myType&)
{
return sizeof(myType::value_type);
}
 
B

Bastien Chevreux

Juha said:
The standard way to get the value type of an STL container is
container::value_type. Thus you can do this:

template<class myType>
size_t sizeOfElem(myType&)
{
return sizeof(myType::value_type);
}

There, I knew I had overseen something. Thank you very much.

Regards,
Bastien
 
J

James Kanze

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?

As Juha said, the normal way would be with
Container::value_type. But just for the record, the operand of
sizeof is guaranteed not to be evaluated, so there's no
undefined behavior in your solution either.
 
J

Juha Nieminen

James said:
As Juha said, the normal way would be with
Container::value_type. But just for the record, the operand of
sizeof is guaranteed not to be evaluated, so there's no
undefined behavior in your solution either.

So basically "sizeof(container.front())" would be "what is the size of
the return value type of the member function front() of this type"?
 
B

Bo Persson

Juha said:
So basically "sizeof(container.front())" would be "what is the
size of the return value type of the member function front() of
this type"?

Yes.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top