enum class E {
One,
Two
};
void foo(E value = decltype(value)::One) {
}
它可以使用Clang(3.9)进行编译,但不能使用GCC 6.1进行编译:value was not declared in this scope
。
哪个编译器是正确的?
enum class E {
One,
Two
};
void foo(E value = decltype(value)::One) {
}
它可以使用Clang(3.9)进行编译,但不能使用GCC 6.1进行编译:value was not declared in this scope
。
哪个编译器是正确的?
decltype
中使用它怎么样?措辞已经过时并无意中禁止了它。请参见core issue 2082:
According to 8.3.6 [dcl.fct.default] paragraph 9,
A default argument is evaluated each time the function is called with no argument for the corresponding parameter. The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in a default argument, even if they are not evaluated. This prohibits use of parameters in unevaluated operands, e.g.,
void foo(int a = decltype(a){});
This wording predates the concept of “unevaluated operands” (the phrase “not evaluated” refers to calls to the function where an actual argument is supplied and thus the default argument is not used, not to unevaluated operands) and should not apply to such cases.
因此,引用段落已被修改为:
参数不得出现在默认参数的潜在求值表达式中。
由于decltype
的操作数是未求值的,所以现在这样做是可以的,而GCC是错误的。
int h(int a, int b = sizeof(a));
在一个示例中被用作错误,因此我认为这是不允许的。 - krzaq