本文介绍了使用std :: acos和clang ++而不是g ++的Constexpr编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to experiment with migrating a project from gcc to clang++. I admit ignorance on my part, I'm not sure why the following bit of code

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

使用g ++进行静默编译,但clang ++会产生错误

compiles silently with g++ but clang++ produces the error

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

我希望有人能比我更能启发我。

and I was hoping someone who knows more about it than I do could enlighten me.

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

NB: Tried with -std=C++14 and C++1y. Fails under clang version 3.6.2 (tags/RELEASE_362/final). Works with g++ (GCC) 5.2.0.

推荐答案

此处的Clang是正确的,我们不允许使用 acos 的常量表达式。

Clang is correct here, we are not allowed to use acos in a constant expression.

问题是在标准中未标记为constexpr,而是。这是,最终应在gcc中修复。

The issue is that acos is not marked constexpr in the standard but gcc treats some functions not marked in the standard including acos as constexpr. This is a non-conforming extension and should eventually be fixed in gcc.

通常用于固定折叠,我们可以看到我们是否在gcc中使用 -fno-builtin 禁用了这种不符合要求的行为,我们将收到以下错误消息:

Builtin functions are often used to constant fold and we can see if we use -fno-builtin with gcc it disables this non-conforming behavior and we will receive the following error:

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

这篇关于使用std :: acos和clang ++而不是g ++的Constexpr编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 16:01