我刚刚尝试在 Ubuntu 13.04 上使用带有 GCC 4.7.3 标准库头文件的 clang 3.3 编译大量代码。这一切都很顺利,除了一个问题。这段代码已经在这台机器上用标准的 Ubuntu clang 3.2 包编译,所以我假设这是 clang 3.3 编译器的一些变化。与使用复杂 header 的 const 和 constexpr 有关的问题。特别是复杂类型具有以下代码块
#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
在我的编译中,我输入了第一段代码,所以编译器看到了
constexpr double real() { return __real__ _M_value; }
这导致 clang 产生一个错误,即真正的成员函数不是 const 的,下面是
/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; }
我已阅读以下帖子 Difference between `constexpr` and `const`和其他一些类似的文件,但我仍然不清楚这是 GCC 头文件问题还是 clang 编译器问题。我的感觉是标记为 constexpr 的成员函数应该被编译器视为 const,在这种情况下 clang 是错误的。
最佳答案
根据status page对于 clang,N3652 Relaxing requirements on constexpr functions部分实现。这篇论文做了很大的改变。以下段落已被删除。
A constexpr specifier for a non static member function that is not a constructor declares that member function to be const (9.3.1).
此更改意味着您的函数不能再在 const
对象上调用。另请参阅 Fixing constexpr member functions without const这是修复图书馆这些区域的提案。
关于c++ - clang 3.3 和 GCC 4.7 const v 的 constexpr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17245050/