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

Re: copy initialization

35 views
Skip to first unread message
Message has been deleted

Paavo Helde

unread,
Apr 24, 2015, 12:36:01 AM4/24/15
to
r...@zedat.fu-berlin.de (Stefan Ram) wrote in news:initialization-
2015042...@ram.dialup.fu-berlin.de:

> People told me that
>
>::std::string s( ... )
>
> was »direct initialization« and
>
>::std::string s = ...
>
> was »copy initialization«.
>
> Herb Sutter wrote »Copy initialization means the object is
> initialized using the copy constructor«.

So what? From a sentence like "Car washing is using water" you cannot
infer "Any usage of water means car washing".

>
> I believe that in the case
>
>::std::string t;
>::std::string s( t );
>
> , the object »s« is initialized using the copy constructor.
> So then this is copy initialization, even if it uses
> parentheses?

This is terminology nitpicking; it boils down to whether you like to
think about the copy constructor in this case as a "single constructor
needed for constructing s from t" or "a copy initialization stage after
skipping the unneeded conversion of t into type of s". I guess logically
it could mean both. Fortunately, it seems latest C++ standards have given
up logical reasoning in this area and only talk about the *form* of the
statement (8.5/16, [dcl.init]):

"The initialization that occurs in the forms
T x(a);
T x{a};
as well as in new expressions (5.3.4), static_cast expressions (5.2.9),
functional notation type conversions (5.2.3), and base and member
initializers (12.6.2) is called direct-initialization."

So this is direct initialization, full stop.

Cheers
Paavo

Message has been deleted

Norman J. Goldstein

unread,
Apr 24, 2015, 6:23:40 PM4/24/15
to
On 04/23/2015 03:36 PM, Stefan Ram wrote:
> People told me that
>
> ::std::string s( ... )
>
> was »direct initialization« and
>
> ::std::string s = ...
>
> was »copy initialization«.
>
> Herb Sutter wrote »Copy initialization means the object is
> initialized using the copy constructor«.
>
> I believe that in the case
>
> ::std::string t;
> ::std::string s( t );
>
> , the object »s« is initialized using the copy constructor.
> So then this is copy initialization, even if it uses
> parentheses?
>
The two statements

::std::string s( "hello" );

and

::std::string s = "hello";

may, or may not, be compiled into the same code. It is up to the
compiler to decide whether to do the 2nd one as direct, or via a copy
constructor.

I'm not sure if the concept of "direct initialization" is well-defined,
but I would take it to mean that no constructors may be eliminated from
the generated code.
0 new messages