Pushing back a new struct on a list/vector

Discussion in 'C++' started by eriwik@student.chalmers.se, Nov 22, 2006.

  1. Guest

    Consider this:

    #inlcude <list>

    struct elem {
    int col;
    int val;
    };

    int main() {
    std::list<elem> l;

    l.push_back(elem e={1,1}); // Error

    elem e = {1,1};
    l.push_back(e); // OK

    return 0;
    }

    Is there any way to make the non-working line work without adding a
    constructor to elem? What I want to do is to create an anonymous struct
    and pass it as an argument to a function using initialization. It's not
    particulary important, it's just been bugging me for some time now.

    --
    Erik Wikström
    , Nov 22, 2006
    #1
    1. Advertising

  2. * :
    > Consider this:
    >
    > #inlcude <list>
    >
    > struct elem {
    > int col;
    > int val;
    > };
    >
    > int main() {
    > std::list<elem> l;
    >
    > l.push_back(elem e={1,1}); // Error
    >
    > elem e = {1,1};
    > l.push_back(e); // OK
    >
    > return 0;
    > }
    >
    > Is there any way to make the non-working line work without adding a
    > constructor to elem?


    No. And why would you want to?


    > What I want to do is to create an anonymous struct
    > and pass it as an argument to a function using initialization.


    You might consider using std::pair, or (not yet standard) std::tr1::tuple.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 22, 2006
    #2
    1. Advertising

  3. mimi Guest

    " 写é“:
    "
    > Consider this:
    >
    > #inlcude <list>
    >
    > struct elem {
    > int col;
    > int val;
    > };
    >
    > int main() {
    > std::list<elem> l;
    >
    > l.push_back(elem e={1,1}); // Error
    >
    > elem e = {1,1};
    > l.push_back(e); // OK
    >
    > return 0;
    > }
    >
    > Is there any way to make the non-working line work without adding a
    > constructor to elem?

    I think adding a constructor is required. You could use std::pair, but
    it provides a constructor also, not by you, but by the STL.
    > What I want to do is to create an anonymous struct
    > and pass it as an argument to a function using initialization. It's not
    > particulary important, it's just been bugging me for some time now.
    >
    > --
    > Erik Wikström
    mimi, Nov 22, 2006
    #3
  4. David Harmon Guest

    On 22 Nov 2006 01:07:20 -0800 in comp.lang.c++,
    "" <> wrote,
    >Is there any way to make the non-working line work without adding a
    >constructor to elem? What I want to do is to create an anonymous struct
    >and pass it as an argument to a function using initialization.


    Sorry, no way to do that, but you can make it nearly anonymous by
    limiting the scope of the very short-lived name.

    {elem e = {1,1}; l.push_back(e);}
    David Harmon, Nov 22, 2006
    #4
    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. hall
    Replies:
    4
    Views:
    431
  2. pmatos
    Replies:
    6
    Views:
    23,727
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,353
    Chris Fogelklou
    Apr 20, 2004
  4. Replies:
    8
    Views:
    1,891
    Csaba
    Feb 18, 2006
  5. ehui928
    Replies:
    2
    Views:
    458
    ehui928
    May 29, 2006
Loading...

Share This Page