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

Re: Ways to write initializations

69 views
Skip to first unread message
Message has been deleted
Message has been deleted

Victor Bazarov

unread,
Apr 18, 2015, 7:28:11 PM4/18/15
to
On 4/18/2015 2:43 PM, Stefan Ram wrote:
> Let T be a class. Now I compare these declarations:
>
> T x;
> T x();
> T x{};
>
> Do I get this right now:
>
> »T x();« declares a function, and »T x;« and »T x{};« both
> declare and define a variable x using the default
> constructor, that is, there is not difference in the
> effect/meaning of »T x;« and »T x{};«?
>
> Even when there is an initializier-list constructor
> it will not be called in the case of »T x{};«, but the
> default-constructor wil be called?
>
> And then, »T x{};« is called a »value-initialization« IIRC.
> Is there any rationale for this designation? After all, a
> value is what actually is /missing/ in the braces!

The difference exists when the type T is not a class type, IIRC. For
instance, if 'T' is an arithmetic type, 'x' will be left uninitialized
if you write

T x;

whereas if you write

T x{};

it's value-initialized, which for arithmetic types means zero-initialized.

> And then comparing:
>
> T x( 2, "a" );
> T x{ 2, "a" };
>
> Do I get this right now:
>
> When there is no initializer-list constructor, both
> declarations have the same meaning. But when there is an
> initializier-list constructor, this initializer-list
> constructor will be called for the declaration
> »T x{ 2, "a" };«, but not for »T x( 2, "a" );«?

Yes, I think you did get it right. Have you tried it?

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

Victor Bazarov

unread,
Apr 18, 2015, 7:31:38 PM4/18/15
to
On 4/18/2015 4:02 PM, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> T x( 2, "a" );
>
> Another question:
>
> T x = ...;
>
> is a copy initialization IIRC, while
>
> T x( ... )
>
> is a direct initialization IIRC.

(if it *is* initialization, and not a declaration of a function) :-)

> Is there any case where one would prefer to write a copy
> initialization instead of a direct initialization?

In some cases it's easier to read the '=' form. Especially as far as
built-in types are concerned. Older programmers (and C programmers) are
used to seeing forms

T x = expression;

rather than

T x{expression};

For built-in types those mean the same thing.

peter koch

unread,
Apr 20, 2015, 4:44:35 AM4/20/15
to
Søndag den 19. april 2015 kl. 01.31.38 UTC+2 skrev Victor Bazarov:
> On 4/18/2015 4:02 PM, Stefan Ram wrote:
[...]
>
> > Is there any case where one would prefer to write a copy
> > initialization instead of a direct initialization?
>
> In some cases it's easier to read the '=' form. Especially as far as
> built-in types are concerned. Older programmers (and C programmers) are
> used to seeing forms
>
> T x = expression;
>
> rather than
>
> T x{expression};
>
> For built-in types those mean the same thing.

Not quite. Consider:

int i = 3.14;
int i = { 3.14 };

The first one succeeds, where the second one gives a compiler error.

/Peter

Victor Bazarov

unread,
Apr 20, 2015, 9:03:36 AM4/20/15
to
I am not sure why you have combined the two forms in the latter
statement and expect it to work. Shouldn't the comparison have been

int i = 3.14;

with

int i{3.14};

?

peter koch

unread,
Apr 20, 2015, 10:14:27 AM4/20/15
to
Mandag den 20. april 2015 kl. 15.03.36 UTC+2 skrev Victor Bazarov:
> On 4/20/2015 4:44 AM, peter koch wrote:
[snip]
> >
> > int i = 3.14;
> > int i = { 3.14 };
> >
> > The first one succeeds, where the second one gives a compiler error.
>
> I am not sure why you have combined the two forms in the latter
> statement and expect it to work. Shouldn't the comparison have been
>
> int i = 3.14;
>
> with
>
> int i{3.14};
>
Yup - you are right. The equal-sign was the result of an incorrect copy-paste.

/Peter
Message has been deleted
0 new messages