Clang 3.3 and GCC 4.7 const v constexpr

I just tried to build a rather large chunk of code using clang 3.3 with the header files of the GCC 4.7.3 standard library on Ubuntu 13.04. Everything went well, except for one question. This code already compiles with the standard Ubuntu clang 3.2 package on this computer, so I assume these are some changes to the clang 3.3 compiler. The problem is with const and constexpr using a complex header. In particular, a complex type has the following code block

#ifdef __GXX_EXPERIMENTAL_CXX0X__ // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 387. std::complex over-encapsulated. constexpr double real() { return __real__ _M_value; } constexpr double imag() { return __imag__ _M_value; } #else double& real() { return __real__ _M_value; } const double& real() const { return __real__ _M_value; } double& imag() { return __imag__ _M_value; } const double& imag() const { return __imag__ _M_value; } #endif 

In my compilation, I enter the first block of code and so the compiler sees

 constexpr double real() { return __real__ _M_value; } 

This causes clang to create an error that the actual member function is not const with the following

 /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/complex:1212:7: note: candidate function not viable: 'this' argument has type 'const complex<double>', but method is not marked const real() { return __real__ _M_value; } 

I read the following post The difference between `constexpr` and` const` and several other similar documents, but I still do not quite understand if this is a GCC header problem or a clan compiler problem. I feel that the member function designated by constexpr should be considered by the compiler as const, in which case clang is wrong.

+6
source share
1 answer

In accordance with the status page for clang N3652, requirements for satisfying constexpr functions are partially implemented . This article has changed a lot. The following snippet has been deleted.

The constexpr specifier for a non-stationary member function that is not a constructor declares that the member function is a constant (9.3.1).

This change means that your function can no longer be called on const objects. Also, see Fixing constexpr member functions without const , which is a suggestion to fix those areas of the library.

+13
source

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


All Articles