具有不变的非constexpr参数的constexpr:谁是正确的,c还是gcc? [英] constexpr with untouched non-constexpr arguments: Who is correct, clang or gcc?

查看:39
本文介绍了具有不变的非constexpr参数的constexpr:谁是正确的,c还是gcc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个测试用例,并且我相信它们都是有效的:

I have 4 test cases and I believe that all of them are valid:

constexpr int f(int const& /*unused*/){
    return 1;
}

void g(int const& p){
    constexpr int a = f(p); // clang error, gcc valid

    int v = 0;
    constexpr int b = f(v); // clang valid, gcc valid

    int const& r = v;
    constexpr int c = f(r); // clang error, gcc error

    int n = p;
    constexpr int d = f(n); // clang valid, gcc valid
}

int main(){
    int p = 0;
    g(p);
}

Clang和GCC仅在第一个测试用例中有所不同。

Clang and GCC differ only in the first test case.

我用clang 4&

I tested with clang 4 & 5 (20170319) and with GCC 7.0.1 (20170221).

5(20170319)和GCC 7.0.1(20170221)。

If I'm right it would massively simplify the usage of boost::hana in static_assert's.

如果我是对的,它将大大简化在static_assert中使用boost :: hana的过程。 p>

解决方案

推荐答案

[expr.const] / 2


表达式 e 核心常量表达式,除非按照抽象机的规则对 e 进行
评估,否则
计算以下表达式之一:

  • [...]
  • an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and either


  • [...]

  • 引用引用类型的变量或数据成员的 id-expression ,除非引用具有先前的初始化并且
    都具有


    • 它使用常量表达式初始化,或者

    • it is initialized with a constant expression or

    其生存期始于对的求值e ;

    p r 。因此 f(p) f(r)都不是核心常量表达式,因此都不能用于初始化 constexpr 变量。叮当是正确的。

    Neither condition is satisfied for p or r. Therefore neither f(p) nor f(r) is a core constant expression and hence neither can be used to initialize a constexpr variable. Clang is correct.

    这篇关于具有不变的非constexpr参数的constexpr:谁是正确的,c还是gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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