In-class member initializer using a constructor: is it allowed?

Constructor

I recently found an interesting piece of code in the article Get to Know the New C++11 Initialization Forms by Danny Kalev:

class C
{
string s("abc");
double d=0;
char * p {nullptr};
int y[5] {1,2,3,4};
public:
C();
};

The line string s("abc"); seems suspicious to me. I thought that using a constructor is not allowed while a member is initialized in-class. And this code (simplified to class C { string s("abc"); };`) doesn't compile with

  • clang 3.6.1 (compiler arguments are -std=c++11 -Wall -Wextra -Werror -pedantic-errors)
  • g++ 5.1.0 (compiler arguments are the same: -std=c++11 -Wall -Wextra -Werror -pedantic-errors)
  • vc++ 18.00.21005.1 (compiler arguments are /EHsc /Wall /wd4514 /wd4710 /wd4820 /WX /Za)
  • vc++ 19.00.22929.0 (compiler arguments are predefined by the service: /EHsc /nologo /W4 /c)

Am I right and there is an error in this article?

0x499602D2

Am I right and there is an error in this article?

Yes, it is an error in the article.

Only a brace-or-equal-initializer is allowed in the declaration of a data member. The initializations of d, p, and y are correct, but not s. The rationale for this is that using an expression-list would ambiguate the declaration with a function declaration and it would also cause conflicts with name lookup in the class body.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Constructor definition with a member initializer list

From Dev

Child Class constructor using private member

From Dev

Starting a member function in a thread using class constructor

From Dev

In-class member initialization with an initializer list using uniform initialization syntax?

From Dev

Class constructor: Incomplete type is not allowed (in member list) - VS C++ w/ QT

From Dev

C++ Undefined reference when using class member in constructor

From Dev

In-class member initializer fails with VS 2013

From Dev

Initialize std::tuple in member initializer list of a class

From Dev

C++11 member initializer list vs in-class initializer?

From Dev

Calling member constructor in class definition

From Dev

Const Class Member Copy Constructor

From Dev

Why is it allowed for a parent class object to be instantiated using a child class's constructor?

From Dev

Why is it allowed for a parent class object to be instantiated using a child class's constructor?

From Dev

Initialize parent class with empty constructor in initializer list?

From Dev

Populating std::array in class' constructor initializer

From Dev

Initialize parent class with empty constructor in initializer list?

From Dev

Dart. Child class and constructor initializer list

From Dev

using keyword with class not allowed?

From Dev

Why initialization of const char array member incompatible in constructor initializer?

From Dev

Initializer list syntax in member initializer list using C++11

From Java

Error when using in-class initialization of non-static data member and nested class constructor

From Dev

C++populating base class protected member using derived class constructor

From Dev

Passing class member to base class constructor (by reference)

From Dev

copy constructor for class with pointers as member variable

From Dev

Why a member method of class is called before the Constructor

From Dev

Initialize tuple member in constructor of variadic template class

From Dev

Bind virtual class member function in base constructor

From Dev

Instantiating an object as class member without default constructor

From Dev

Default constructor for a class with a reference data member?

Related Related

  1. 1

    Constructor definition with a member initializer list

  2. 2

    Child Class constructor using private member

  3. 3

    Starting a member function in a thread using class constructor

  4. 4

    In-class member initialization with an initializer list using uniform initialization syntax?

  5. 5

    Class constructor: Incomplete type is not allowed (in member list) - VS C++ w/ QT

  6. 6

    C++ Undefined reference when using class member in constructor

  7. 7

    In-class member initializer fails with VS 2013

  8. 8

    Initialize std::tuple in member initializer list of a class

  9. 9

    C++11 member initializer list vs in-class initializer?

  10. 10

    Calling member constructor in class definition

  11. 11

    Const Class Member Copy Constructor

  12. 12

    Why is it allowed for a parent class object to be instantiated using a child class's constructor?

  13. 13

    Why is it allowed for a parent class object to be instantiated using a child class's constructor?

  14. 14

    Initialize parent class with empty constructor in initializer list?

  15. 15

    Populating std::array in class' constructor initializer

  16. 16

    Initialize parent class with empty constructor in initializer list?

  17. 17

    Dart. Child class and constructor initializer list

  18. 18

    using keyword with class not allowed?

  19. 19

    Why initialization of const char array member incompatible in constructor initializer?

  20. 20

    Initializer list syntax in member initializer list using C++11

  21. 21

    Error when using in-class initialization of non-static data member and nested class constructor

  22. 22

    C++populating base class protected member using derived class constructor

  23. 23

    Passing class member to base class constructor (by reference)

  24. 24

    copy constructor for class with pointers as member variable

  25. 25

    Why a member method of class is called before the Constructor

  26. 26

    Initialize tuple member in constructor of variadic template class

  27. 27

    Bind virtual class member function in base constructor

  28. 28

    Instantiating an object as class member without default constructor

  29. 29

    Default constructor for a class with a reference data member?

HotTag

Archive