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

Comment on stackoverflow about value initialization seems to contradict my gcc compiler.

45 views
Skip to first unread message

Paul

unread,
Jan 13, 2017, 6:55:12 AM1/13/17
to
The text at the end of this message is from stackoverflow,
and it seems to say that C c = C();
value initializes c without calling the default constructor. I thought
that C() did call the default constructor. For example, I just tried to
compile the code:
struct C
{
C(int ){}
int x;
};
int main()
{
C c = C();
}

I got a compiler error saying that C::C() has no matching function. This
compiler message is exactly what I expected. But it seems that
the passage below says that C c = C(); is fine and has the effect
of value-initializing c.x Is the below passage just wrong?
Please note that I'm happy with the way my compiler behaved. My problem
is with the below passage. Many thanks for your help.

Paul

BEGIN QUOTE
Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types.

However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means. Not by default constructor, nor by constructor at all.

For example, there's a widespread incorrect belief that for class C the syntax C() always invokes default constructor. In reality though, the syntax C() performs so called value-initialization of the class instance. It will only invoke the default constructor if it is user-declared. (That's in C++03. In C++98 - only if the class is non-POD). If the class has no user-declared constructor, then the C() will not call the compiler-provided default constructor, but rather will perform a special kind of initialization that does not involve the constructor of C at all. Instead, it will directly value-initialize every member of the class. For built-in types it results in zero-initialization.
END QUOTE

Alf P. Steinbach

unread,
Jan 13, 2017, 8:20:58 AM1/13/17
to
On 13.01.2017 12:55, Paul wrote:
> The text at the end of this message is from stackoverflow,
> and it seems to say that C c = C();
> value initializes c

Yes.


> without calling the default constructor.

In the case where there is no default constructor.


> I thought that C() did call the default constructor.

It can't do that when there isn't one.


> For example, I just tried to
> compile the code:
> struct C
> {
> C(int ){}
> int x;
> };
> int main()
> {
> C c = C();
> }
>
> I got a compiler error saying that C::C() has no matching function. This
> compiler message is exactly what I expected.

Right.


Cheers & hth.,

- Alf

Louis Krupp

unread,
Jan 13, 2017, 4:53:36 PM1/13/17
to
On Fri, 13 Jan 2017 03:55:01 -0800 (PST), Paul <peps...@gmail.com>
wrote:
The key wording seems to be "If the class has no user-declared
constructor, then the C() will ... perform a special kind of
initialization that does not involve the constructor of C at all." In
your example, C *has* a user-declared constructor, C(int), so
value-initialization doesn't happen.

I tried this with g++:

struct C
{
C(int ){}
};
int main()
{
C b;
==> error: no matching function for call to ‘C::C()’

C c = C();
==> error: no matching function for call to ‘C::C()’

}

If I remove the constructor C(int), the errors go away.

Louis

Paul

unread,
Jan 13, 2017, 5:14:33 PM1/13/17
to
Thanks to both of you. I understand it now.

Paul

Andrey Tarasevich

unread,
Jan 20, 2017, 9:05:12 PM1/20/17
to
On 1/13/2017 3:55 AM, Paul wrote:
> The text at the end of this message is from stackoverflow,
> and it seems to say that C c = C();
> value initializes c without calling the default constructor.

You misunderstood the context of the SO answer (of which I'm the author).

Note that the question itself talks about compiler-provided default
constructor, which implies that compiler-provided default constructor
_exists_. This already means that it not applicable to your example,
since in your example the compiler-provided default constructor does not
exist at all. By defining your `C::C(int)` constructor you immediately
suppressed the implicit declaration of `C::C()`. End of story.

So, the answer on SO implies that we are talking about situations when
the compiler-provided default constructor exists, which makes the `()`
initializer valid. (In your case it is simply invalid.)

--
Best regards,
Andrey Tarasevich

Manfred

unread,
Jan 22, 2017, 9:53:20 AM1/22/17
to
To get back at the original question, and in the correct context of a
compiler-provided default constructor, it looks to me that there is a
small note to your answer:

[quote]If the class has no user-declared constructor, then the C() will
not call the compiler-provided default constructor, but rather will
perform a special kind of initialization that does not involve the
constructor of C at all. Instead, it will directly value-initialize
every member of the class. For built-in types it results in
zero-initialization.[/quote]

You are probably referring to § 8.5, clause (8.2) (here from n4296):

"8 To value-initialize an object of type T means:
<...>
(8.2) — if T is a (possibly cv-qualified) class type without a
user-provided or deleted default constructor, then the object is
zero-initialized and the semantic constraints for default-initialization
are checked, and if T has a non-trivial default constructor, the object
is default-initialized;
<...>"

As you see, after zero-initialization, "if T has a non-trivial default
constructor, the object is default-initialized", which is a bit
different than "does not involve the constructor of C at all", and it
could not be otherwise, for a non-trivial default constructor.

The subsequent statement of the standard:

"An object that is value-initialized is deemed to be constructed and
thus subject to provisions of this International Standard applying to
“constructed” objects, objects “for which the constructor has
completed,” etc., even if no constructor is invoked for the object’s
initialization."
explicitly applies /if/ no constructor is invoked.

0 new messages