【发布时间】:2017-04-22 07:34:04
【问题描述】:
我最近发现这段代码在 GCC 和 MSVC 中都能正常编译:
auto foo = [](...){
cout << "foo() called" << endl;
};
它接受任意数量的任何类型的参数,并且对这些参数不做任何事情,所以它的工作方式就像 auto 放在 ... 之前一样:
// All of these lines will call the lambda function
foo();
foo(100);
foo("Test");
foo("Testing", 1, 2, 3);
lambda functions 上的 C++ 参考似乎没有提及这一点,parameter packs 上的页面也没有提及。
更令人惊讶的是,这无法编译:
auto foo = [](... x){ // compile error
cout << "foo() called" << endl;
};
这种行为是否由标准规定,如果是,为什么前者编译而后者失败?
【问题讨论】:
-
这说明了一切!
标签: c++ lambda c++14 variadic-templates auto