[英]Access template class parameter in default lambda argument
I'm writing a simple universal pool. 我正在编写一个简单的通用池。 The template class takes a parameter in the constructor that is a factory function to instantiate objects in the pool as needed.
模板类在构造函数中采用一个参数,该参数是一种工厂函数,可根据需要实例化池中的对象。
template<typename T>
struct Foo {
std::function <T*()> factory_;
Foo(std::function<T*()> factory): factory_(factory) {}
};
This compiles fine, but I wanted to make a default argument for the constructor using lambda: 这样编译可以,但是我想使用lambda为构造函数设置一个默认参数:
Foo(std::function<T*()> factory = [](){return new T();} ): factory_(factory) {}
This does not compile - it says that T is unknown. 这不会编译-它表示T未知。 Is there a way to make lambda expressions aware of class template parameters?
有没有办法让lambda表达式了解类模板参数? I tried using
typedef
, but to no avail. 我尝试使用
typedef
,但无济于事。
This should work as proposed, however you might have hit a bug in VC++ (it's not exactly C++11 ready yet). 这应该按照建议的方式工作,但是您可能已经在VC ++中遇到了一个错误(尚未完全支持C ++ 11)。
As a workaround, you might try to replace your lambda by a private static method instead. 解决方法是,您可以尝试使用私有的静态方法替换lambda。
template <typename T>
class LIFOPool
{
std::function <T*()> factory_;
//...details irrelevant
static T* DefaultMake() { return new T{}; }
public:
LIFOPool(std::function<T*()> factory = DefaultMake) : factory_(factory) {}
//...details irrelevant
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.