for (auto x : {exp1,exp2,exp3}) ?

Discussion in 'C++' started by Andrew Tomazos, Dec 16, 2011.

  1. Is the following valid C++11? Im getting a seg fault on it from gcc
    4.6.1.

    #include <set>
    #include <string>
    #include <iostream>

    using namespace std;

    int main()
    {
    set<string> ss;
    ss.insert("a");
    ss.insert("b");
    ss.insert("c");

    for (auto s1 : ss)
    for (auto s2 : {s1 + "x", s1 + "y", s1 + "z"} )
    cout << s2 << endl;

    return 0;
    }
    Andrew Tomazos, Dec 16, 2011
    #1
    1. Advertising

  2. Andrew Tomazos <> wrote:
    > set<string> ss;
    > ss.insert("a");
    > ss.insert("b");
    > ss.insert("c");
    >
    > for (auto s1 : ss)
    > for (auto s2 : {s1 + "x", s1 + "y", s1 + "z"} )
    > cout << s2 << endl;


    I must admit that I don't know enough C++11 to tell for sure, but
    I'd imagine that the type of 's2' resolves to an initializer list
    containing std::string *references*, which point to temporary objects
    that are destroyed by the time the body of the loop is executed the
    first time. (This would be very different when eg. constructing a
    vector with such an initializer list, because the temporaries would
    be destroyed only after the constructor call ends.)

    I may be completely wrong, though, because at this juncture I really
    don't know what the type of '{ s1 + "x" }' is.
    Juha Nieminen, Dec 16, 2011
    #2
    1. Advertising

  3. On Dec 16, 1:06 pm, Juha Nieminen <> wrote:
    > Andrew Tomazos <> wrote:
    > >        set<string> ss;
    > >        ss.insert("a");
    > >        ss.insert("b");
    > >        ss.insert("c");

    >
    > >        for (auto s1 : ss)
    > >                for (auto s2 : {s1 + "x", s1 + "y", s1 +"z"} )
    > >                        cout << s2 << endl;

    >
    >   I must admit that I don't know enough C++11 to tell for sure, but
    > I'd imagine that the type of 's2' resolves to an initializer list
    > containing std::string *references*, which point to temporary objects
    > that are destroyed by the time the body of the loop is executed the
    > first time. (This would be very different when eg. constructing a
    > vector with such an initializer list, because the temporaries would
    > be destroyed only after the constructor call ends.)
    >
    >   I may be completely wrong, though, because at this juncture I really
    > don't know what the type of '{ s1 + "x" }' is.


    I think you're right, the seg fault is a memcpy of string in invalid
    memory.

    When I make the following change...

    -                for (auto s2 : {s1 + "x", s1 + "y", s1 + "z"} )
    +                for (auto s2 : set<string> {s1 + "x", s1 +"y", s1 +
    "z"} )

    ....it works. I suspect this constructs a temporary set<string> that
    holds the expressions in the init list in memory for the lifetime of
    the loop.

    I'm still partially confused why the initializer_list<string> doesn't
    do this itself. I guess it needs to be a list of rvalue references in
    order to do its other magic.
    -Andrew.
    Andrew Tomazos, Dec 16, 2011
    #3
  4. Andrew Tomazos

    Marc Guest

    Andrew Tomazos wrote:

    > Is the following valid C++11? Im getting a seg fault on it from gcc
    > 4.6.1.


    g++-4.6 executes the destructors before starting the loop.
    g++-4.7 executes them at the end of the loop and is happy with the code.
    It seems more likely that g++-4.7 is right :)

    > #include <set>
    > #include <string>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main()
    > {
    > set<string> ss;
    > ss.insert("a");
    > ss.insert("b");
    > ss.insert("c");
    >
    > for (auto s1 : ss)
    > for (auto s2 : {s1 + "x", s1 + "y", s1 + "z"} )
    > cout << s2 << endl;
    >
    > return 0;
    > }
    Marc, Dec 16, 2011
    #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. =?Utf-8?B?QnJ1Y2UgUHVsbHVt?=

    DataGrid Row Auto-Size

    =?Utf-8?B?QnJ1Y2UgUHVsbHVt?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,002
  2. =?Utf-8?B?V2FyYW4=?=

    Auto-Suggested Textbox like google auto suggest

    =?Utf-8?B?V2FyYW4=?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,485
    inrakeshworld
    Jul 27, 2007
  3. linkswanted
    Replies:
    1
    Views:
    862
  4. Replies:
    6
    Views:
    389
    user923005
    Jun 7, 2008
  5. Qi
    Replies:
    1
    Views:
    343
Loading...

Share This Page