clang version 5.0.0 (trunk 305664)
Target: x86_64-unknown-linux-gnu
以下代码已经成功编译:
template <int... A>
void f() {
([](auto) {
if constexpr (A == 0)
return 42;
else
return 3.14;
}(0), ...);
}
int main() {
f<0, 1>();
}
……但是这个例子却没有:
template <int... A>
void f() {
([](auto...) { // Variadic lambda
if constexpr (A == 0)
return 42;
else
return 3.14;
}(), ...); // No argument
}
int main() {
f<0, 1>();
}
...产生:
<source>:7:13: error: 'auto' in return type deduced as 'double' here but deduced as 'int' in earlier return statement
return 3.14;
^
<source>:3:6: note: in instantiation of function template specialization 'f()::(anonymous class)::operator()<>' requested here
([](auto...) { // Variadic lambda
^
<source>:12:5: note: in instantiation of function template specialization 'f<0, 1>' requested here
f<0, 1>();
^
我不会期望一个空的参数包和一个虚拟(dummy)参数之间有不同的行为。
这种差异有原因吗?还是这是编译器的错误?
if constexpr
的条件必须是依赖的? - ascheplermain
函数。 - Quentin