constexpr编译错误与clang ++不g ++ [英] Constexpr compile error with clang++ not g++

查看:1025
本文介绍了constexpr编译错误与clang ++不g ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实验将一个项目从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.

注意:使用-std = C ++ 14和C ++ 1y尝试。在clang版本3.6.2(标签/ 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是正确的,我们不允许使用<$ c $

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

问题是 acos 在标准中未标记为constexpr,但 gcc将标准中未标记的一些函数(包括acos)视为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.

内置函数通常用于不断折叠,我们可以看到是否使用 -fno-builtin 与gcc它禁用此不符合行为,我们将收到以下错误:

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))};
                         ^

这篇关于constexpr编译错误与clang ++不g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆