c – `if constexpr`,内部lambda,内部包扩展 – 编译器错误?

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

我不希望在空参数包和伪参数之间有不同的行为.

是否存在这种差异的原因,或者这是编译器错误

解决方法

我相信这是一个铿锵的错误.

[dcl.spec.auto]中的规则强调我的:

If the declared return type of the function contains a placeholder type,the return type of the function is deduced from non-discarded return statements,if any,in the body of the function ([stmt.if]).

[…]

If a function with a declared return type that contains a placeholder type has multiple non-discarded return statements,the return type is deduced for each such return statement. If the type deduced is not the same in each deduction,the program is ill-formed.

lambda中的一个或另一个return语句被丢弃(如果constexpr被称为废弃语句,则取非分支),只留下一个非废弃的return语句,因此lambda的返回类型应该简单地从中推断出来.一个遗留下来.

此外,clang还可以这样:

template <int A>
void f() {
    [](auto...) {
        if constexpr (A == 0)
            return 42;
        else
            return 3.14;
    }();
}

int main() {  
    f<0>();
    f<1>();
}

所以这可能与lambdas在pack表达式中的工作方式有一些不良的交互.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...