Victor Bazarov
unread,Apr 18, 2015, 7:28:11 PM4/18/15You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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