Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #82973

Re: Understanding default member initializers of constructorless structs

From Andrey Tarasevich <andreytarasevich@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Understanding default member initializers of constructorless structs
Date 2022-02-08 07:29 -0800
Organization A noiseless patient Spider
Message-ID <stu29c$v3$1@dont-email.me> (permalink)
References <sttgpr$1cud$1@gioia.aioe.org>

Show all headers | View raw


On 2/8/2022 2:31 AM, Juha Nieminen wrote:
> I have always understood that the default member initializers (introduced
> in C++11) merely added that particular initialization value to the
> initialization list of a constructor, if it's not explicitly specified
> there. 

Yes, that was the case in C++11. And that was (and is) the initial 
driving idea behind default initializers.

However, in C++11 the accompanying side-effect of this was that 
specifying default initializers for class members immediately killed 
"aggregateness" of the class. In C++11 a class with default initializers 
would support `{}` initializer, but reject non-empty `{ ... }` 
initializers (unless an appropriate user-declared constructor is present).

In C++11:

   struct S
   {
     int a;
     int x = 42;
   };

   int main()
   {
     S s1 = {}; // OK
     S s2 = { 1 }; // ERROR: no such constructor
     S s3 = { 2, 3 }; // ERROR: no such constructor
   }


> All the above is find and dandy, except that the meaning of default member
> initializers seems to be rather different with constructorless structs
> (and classes, I suppose). Consider this:

The above "aggregate defying" behavior of default initializers was 
reconsidered in C++14. Starting from C++14 a class with default 
initializers does not lose its aggregate status and continues to support 
aggregate initialization. And in C++14 aggregate initialization was also 
made aware of default initializers.

> But how does that work, exactly? The structs have no user-declared
> constructors, so where exactly are those default values being "put",
> so to speak? How are the objects constructed from the initializater
> lists?

Aggregate initialization in C++ never used any "constructors" at the top 
level. It is a separate built-in initialization mechanism with its own 
custom specification. So, in C++14 this specification was simply 
modified to take into account default initializers:

8.5.1 Aggregates
7 If there are fewer initializer-clauses in the list than there are 
members in the aggregate, then each member not explicitly initialized 
shall be initialized from its brace-or-equal-initializer or, if there is 
no brace-or-equalinitializer, from an empty initializer list (8.5.4).

-- 
Best regards,
Andrey Tarasevich

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Understanding default member initializers of constructorless structs Juha Nieminen <nospam@thanks.invalid> - 2022-02-08 10:31 +0000
  Re: Understanding default member initializers of constructorless structs "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-02-08 11:55 +0100
    Re: Understanding default member initializers of constructorless structs Juha Nieminen <nospam@thanks.invalid> - 2022-02-08 12:37 +0000
      Re: Understanding default member initializers of constructorless structs red floyd <no.spam.here@its.invalid> - 2022-02-08 10:56 -0800
  Re: Understanding default member initializers of constructorless structs Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-02-08 07:29 -0800
    Re: Understanding default member initializers of constructorless structs Juha Nieminen <nospam@thanks.invalid> - 2022-02-09 07:26 +0000

csiph-web