【发布时间】:2013-04-23 21:30:51
【问题描述】:
这行得通:
#include <functional>
template < bool (*F)( int ) > class Foo {};
bool fooFunc( int n ) { return true; }
int main( int argc, char* argv[] )
{
auto a = Foo< fooFunc >();
}
但这不起作用,因为您无法将 lambda 转换为函数指针:
#include <functional>
template < bool (*F)( int ) > class Foo {};
auto barFunc = [] ( int n ) -> bool { return true; };
int main( int argc, char* argv[] )
{
auto a = Foo< barFunc >();
}
这不起作用,因为您不能使用 std::function 作为模板非类型参数:
#include <functional>
template < std::function< bool( int ) > F > class Bar {};
auto barFunc = [] ( int n ) -> bool { return true; };
int main( int argc, char* argv[] )
{
auto b = Bar< barFunc >();
}
那么如何创建一个能够接受 lambda 封装作为模板非类型参数的模板类?
【问题讨论】:
-
请注意非类型模板参数的含义。你的意思不是简单地放
template <typename T>吗?是不是因为这样做过于放宽了模板参数要求,无法满足您的需求? -
他的意思是一个值(相对于类型)模板参数,例如:
template <int N> -
我认为你试图在编译时做一些在一般情况下直到运行时才能完成的事情,这就是为什么“你不能使用 std::function 作为模板非类型参数”
-
如果从
[](int)->bool到bool(*)(int)的转换是constexpr,这很容易做到。 -
要清楚,如果标准说“没有 lambda 捕获的 lambda 表达式的闭包类型有一个公共的非虚拟非显式 const 转换函数,指向具有相同的函数的指针参数和返回类型作为闭包类型的函数调用运算符。”
constexpr而不是const这将是可行的。但标准是保守的。我想不出为什么转换不应该是constexpr的技术原因,其他人可以吗?
标签: c++ templates c++11 lambda