Constexpr编译错误使用std :: acos和clang ++而不是g ++

时间:2015-09-28 01:43:52

标签: c++ compiler-errors c++14 constexpr clang++

我想尝试将项目从gcc迁移到clang ++。我承认我的无知,我不确定为什么以下的代码

template <typename T>
constexpr T pi{std::acos(T(-1.0))};

用g ++静默编译,但clang ++产生错误

trig.hpp:3:13: error: constexpr variable 'pi<float>' must be initialized by a constant expression
constexpr T pi{std::acos(T(-1.0))};

我希望有人比我更了解它。能够启发我。

注意:尝试使用-std = C ++ 14和C ++ 1y。在clang版本3.6.2(标签/ RELEASE_362 / final)下失败。适用于g ++(GCC)5.2.0。

1 个答案:

答案 0 :(得分:10)

Clang在这里是正确的,我们不允许在常量表达式中使用acos

问题是acos未在标准中标记为constexpr,而是gcc treats some functions not marked in the standard including acos as constexpr。这是一个non-conforming extension,最终应该在gcc中修复。

Builtin functions通常用于常数折叠,我们可以看到我们是否将-fno-builtin与gcc一起使用会禁用此不合规行为,我们会收到以下错误:

error: call to non-constexpr function 'double acos(double)'
constexpr T pi{std::acos(T(-1.0))};
                         ^
相关问题