An incomplete nested static constant. Really C ++ or not?

Clang and GCC disagree on whether the following C++11 code is valid or not:

 struct Thing { int value; static const Thing thing; }; constexpr Thing Thing::thing {3}; 

Clang compiles it, and GNU GCC version 4.7.2 says:
error: redeclaration 'Thing::thing' differs in 'constexpr' constexpr Thing Thing::thing {3};

Which compiler interprets the standard correctly? And is it likely that the C++14 standard will have something new to say about this issue?

+6
source share
1 answer

Introduction

The snippet is legal C ++ 11, and there are no changes in C ++ 14.

Note : gcc 4.9.0 correctly accepts the fragment, i.e. refusing a fragment was a bug in previous versions of the compiler.


Explanation

The constexpr specifier certainly affects the object being defined (it will be declared as const), but, in particular, the specifier indicates that the declared object

  • is a literal type and
  • has an initializer and;
  • that the initializer is a constant expression.


 struct Thing { int value; static const Thing thing; // (A) }; constexpr Thing Thing::thing {3}; // (B) 

This means that the definition of (B) refers to an object of type Thing const with the name thing, which matches (A), therefore; this piece is legal.


7.1.5p9 constexpr [dcl.constexpr] ( n3337 )

A constexpr qualifier used in declaring an object declares the object as const. . Such an object must be of literal type and must be initialized.

+6
source

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


All Articles