What's The Difference Between These Two Statements?

closed account (zb0S216C)
Consider this example:

1
2
3
4
5
class Simple
{
    public:
        Simple( int X, int Y );
};


Now, using the above class, what's the difference between these return statements:

1
2
3
4
5
6
7
8
Simple Basic_Function( )
{
    // Statement A:
    return { 10, 20 };

    // Statement B:
    return( Simple( 10, 20 ) );
}

Are they equivalent? Are any temporaries created implicitly?

Note that both of which compile just fine without compiler extensions.

Wazzak
One works in C++03 and one doesn't. And I believe the first one will get problematic if simple provides a constructor that takes an std::initializer_list<int>, though I'll have to admit that I'm still a bit fuzzy on that.
Is there a reason to use the first option?
closed account (zb0S216C)
@Hanst99: True, C++0x does now define std::initializer_list< >( ) as a type and may cause conflicts in the future. And for that reason, I think I should stick with Statement B.

Thanks for your help, lads :)

Wazzak
Last edited on
In the future? Before C++11 Variant A wasn't possible and it is now the preferred variant.
Uniform initialization has proven to be one of the most time-saving features in C++11.

When a class has a constructor that takes only a single initializer_list (make it explicit to avoid mistakes), it just means you can't use the shorthand if you want to call a different constructor.
Topic archived. No new replies allowed.