lambda中的lambda不能是模板吗?

时间:2017-05-17 16:00:44

标签: c++ templates lambda c++14 auto

Visual Studio编译器(MSVC 2015)无法编译以下简单代码:

int main() {
    auto foo = [](auto callback) {
        callback(int{});
    };
    auto rexs = [&foo]() {
        foo([](auto tg) {});
    };
}

它限制内部编译器错误:

fatal error C1001: An internal error has occurred in the compiler.

VC ++喜欢在编译的代码包含错误时给出错误C1001(即程序员犯了错误,但VC ++只是没有完全识别代码中的错误),所以我想知道我是否可以这里犯了一个错误。

然而,从我可以看到的所有角度来看,我的代码看起来符合标准,对我来说它似乎是一个VC ++错误。我在想什么?

2 个答案:

答案 0 :(得分:2)

您的代码使用clang 3.8和gcc 5.4(http://rextester.com/SCAH69935)编译得很好,所以它似乎是一个VC ++错误。

答案 1 :(得分:-2)

VC++是对的:C ++ 11在lambda中不允许auto。 我们只能在lambda中使用auto,因为C ++ 14。

Clang 3.8.0使用-std=c++11

给出了这个错误
main.cpp:7:17: error: 'auto' not allowed in lambda parameter
    auto f = [](auto x) { return x + 1;};

和gcc 7.1.0(带有相同的标志)给了我这个: main.cpp:在函数' int main()':

main.cpp:7:17: error: use of 'auto' in lambda parameter declaration only available with -std=c++14 or -std=gnu++14

     auto f = [](auto x) { return x + 1;};

代码:

int main()
{
    auto f = [](auto x) { return x + 1;};
}
相关问题