相关疑难解决方法(0)

通用lambda及其参数作为常量表达式

以下代码被GCC 7.2和clang 5.0.0接受,但被Microsoft VS 2017 15.5.0 Preview 5和Intel C++编译器19拒绝:

struct S { };

constexpr int f(S)
{
    return 0;
}

int main()
{
    auto lambda = [](auto x)
    {
        constexpr int e = f(x);
    };

    lambda(S{});
}
Run Code Online (Sandbox Code Playgroud)

微软:

<source>(12): error C2131: expression did not evaluate to a constant
Run Code Online (Sandbox Code Playgroud)

英特尔:

<source>(12): error: expression must have a constant value
    constexpr int e = f(x);
                      ^
<source>(12): note: the value of parameter "x" (declared at line 10) cannot be used as a constant …
Run Code Online (Sandbox Code Playgroud)

c++ lambda language-lawyer c++14

7
推荐指数
1
解决办法
389
查看次数

标签 统计

c++ ×1

c++14 ×1

lambda ×1

language-lawyer ×1