Faced a problem with constexpr

I am having problems initializing a class with constants:

Why does initialization with a pointer to a member in the same class result in an error? The error occurs without using the "Use" class!

class A { private: int a; const int* const aptr; public: constexpr A( int _a): a(_a) , aptr( &a) // why aptr could not be initialized? {} }; class Data { } d1; class B { private: Data* dptr1; public: constexpr B(Data* _p): dptr1( _p) {} }; class Use { static constexpr A a{2}; // fail! error: field initializer is not constant static constexpr B b{&d1}; // works }; 
+6
source share
1 answer

The code is valid, and Klang accepts it; this seems to be a g ++ bug. The Use::aa address is an expression of the address constant, since it evaluates the address of an object with a static storage duration, so it can be used to initialize a constexpr object.

+3
source

Source: https://habr.com/ru/post/944959/


All Articles