C++20是否允许将非捕获lambda衰减为函数指针并直接作为非类型模板参数传递?如果是,正确的语法是什么?
我已尝试在各种版本的clang和gcc中使用-std=c++2a
编写以下代码。
#include <iostream>
template<auto f>
struct S {
static void invoke(int x) { f(x); }
};
using X = S<+[](int x) -> void { std::cout << x << " hello\n"; }>;
int main()
{
X::invoke(42);
}
gcc编译代码没有任何投诉,代码按预期运行。
clang编译失败,并出现以下错误:
error: a lambda expression cannot appear in this context
using X = S<+[](int x) -> void { std::cout << x << " hello\n"; }>;
^
以下是完整代码(在线版本):
Clang 10.0.0 HEAD: https://wandbox.org/permlink/n5eKQ4kQqSpDpr4k
Gcc 10.0.0 HEAD 20200113: https://wandbox.org/permlink/vJ44sdMtwCKAFU64