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

Discussion in 'C++' started by Bastien Chevreux, Jan 10, 2009.

  1. 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
     
    Bastien Chevreux, Jan 10, 2009
    #1
    1. Advertising

  2. Bastien Chevreux wrote:
    > 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);
    }
     
    Juha Nieminen, Jan 10, 2009
    #2
    1. Advertising

  3. Juha Nieminen wrote:
    > 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
     
    Bastien Chevreux, Jan 10, 2009
    #3
  4. Bastien Chevreux

    James Kanze Guest

    On Jan 10, 8:43 pm, Bastien Chevreux <> wrote:

    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 11, 2009
    #4
  5. James Kanze wrote:
    > 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"?
     
    Juha Nieminen, Jan 13, 2009
    #5
  6. Bastien Chevreux

    Bo Persson Guest

    Juha Nieminen wrote:
    > James Kanze wrote:
    >> 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"?


    Yes.


    Bo Persson
     
    Bo Persson, Jan 13, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maitre Bart
    Replies:
    2
    Views:
    526
    Maitre Bart
    Feb 11, 2004
  2. jose luis fernandez diaz
    Replies:
    7
    Views:
    20,222
    Rob Williscroft
    Apr 21, 2004
  3. Replies:
    4
    Views:
    805
    Daniel T.
    Feb 16, 2006
  4. wolverine
    Replies:
    2
    Views:
    454
    Marcus Kwok
    Jul 24, 2006
  5. barcaroller

    begin() for empty STL container

    barcaroller, May 31, 2007, in forum: C++
    Replies:
    2
    Views:
    327
    Robbie Hatley
    May 31, 2007
Loading...

Share This Page