具有不变的非constexpr参数的constexpr:谁是正确的,c还是gcc? [英] constexpr with untouched non-constexpr arguments: Who is correct, clang or 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>
推荐答案
表达式
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
orr
. Therefore neitherf(p)
norf(r)
is a core constant expression and hence neither can be used to initialize aconstexpr
variable. Clang is correct.这篇关于具有不变的非constexpr参数的constexpr:谁是正确的,c还是gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-