Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Initializations involving braces

39 views
Skip to first unread message

Victor Bazarov

unread,
Nov 2, 2015, 9:54:25 AM11/2/15
to
On 11/2/2015 9:24 AM, Stefan Ram wrote:
> How could the following two forms of initialization be
> described?
>
> They are both initializations involving a kind of
> »brace-initializer list«, yet they might possibly be
> different.
>
> #include <initializer_list>
> #include <utility>
> #include <string>
>
> int main()
> { { ::std::pair< int, ::std::string >p{ 7, "alpha" }; }
> { ::std::pair< int, ::std::string >p( { 7, "alpha" } ); }}
>
> Are there any examples where one of these can be used for a
> local variable definition, but not the other?

To me the difference is that the first 'p' is initialized directly from
an "initializer list", whereas with the second 'p' the argument of the
constructor is initialized with an "initializer list".

Since 'std::pair' has no c-tor defined that takes an
'std::initializer_list' object (unlike, say, 'std::vector'), there's
probably some kind of difference here (compared to 'std::vector', for
which there is no difference, methinks), but I am not sure how to wiggle
it out of the Standard document correctly.

V
--
I do not respond to top-posted replies, please don't ask

Alf P. Steinbach

unread,
Nov 2, 2015, 3:42:23 PM11/2/15
to
On 11/2/2015 3:24 PM, Stefan Ram wrote:
> How could the following two forms of initialization be
> described?
>
> They are both initializations involving a kind of
> »brace-initializer list«, yet they might possibly be
> different.
>
> #include <initializer_list>
> #include <utility>
> #include <string>
>
> int main()
> { { ::std::pair< int, ::std::string >p{ 7, "alpha" }; }
> { ::std::pair< int, ::std::string >p( { 7, "alpha" } ); }}
>
> Are there any examples where one of these can be used for a
> local variable definition, but not the other?
>

I think a good guideline for test code is to make it as simple as
possible, but, as reportedly Albert Einstein remarked, no more.

struct S
{
#ifdef NOCOPY
S( S const& ) = delete;
#endif
S( int, char const* ) {}
};

int main()
{
{ S p{ 7, "alpha" }; }
{ S p( { 7, "alpha" } ); }
}

So, both examples are formally direct initialization, but the second one
involves a temporary instance of the class and is /effectively/ a copy
initialization.

The language has become, well not yet too complex for me, but almost.

Cheers & hth.,

- Alf

0 new messages