Chr*_*ger 4 c++ variadic-templates c++11
我正在尝试编写以下工厂类,但我找不到正确的语法:
template<class T, typename... TArgs>
class Factory {
public:
Factory(TArgs... args) {
creator_ = std::bind(&std::make_shared<T, TArgs...>, args...);
// ^^^ some error around here
}
std::shared_ptr<T> Create() const {
return creator_();
}
private:
std::function<std::shared_ptr<T>()> creator_;
};
Run Code Online (Sandbox Code Playgroud)
这就是我使用工厂的方式:
class Foo {
public:
Foo(bool value) {}
};
class Bar {
public:
Bar(const std::string& value) {}
};
Factory<Foo, bool> f1(true);
Factory<Bar, std::string> f2("string");
Run Code Online (Sandbox Code Playgroud)
这些声明时,我得到了错误f1和f2:
error: no match for 'operator=' (operand types are 'std::function<std::shared_ptr<Foo>()>' and 'std::_Bind_helper<false, std::shared_ptr<Foo> (*)(bool&&), bool&>::type {aka std::_Bind<std::shared_ptr<Foo> (*(bool))(bool&&)>}')
creator_ = std::bind(&std::make_shared<T, TArgs...>, args...);
^
error: no match for 'operator=' (operand types are 'std::function<std::shared_ptr<Bar>()>' and 'std::_Bind_helper<false, std::shared_ptr<Bar> (*)(std::basic_string<char>&&), std::basic_string<char, std::char_traits<char>, std::allocator<char> >&>::type {aka std::_Bind<std::shared_ptr<Bar> (*(std::basic_string<char>))(std::basic_string<char>&&)>}')
creator_ = std::bind(&std::make_shared<T, TArgs...>, args...);
^
Run Code Online (Sandbox Code Playgroud)
我必须使用的正确语法是什么std::bind?
std::make_shared 声明如下:
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
Run Code Online (Sandbox Code Playgroud)
因此,std::make_shared<T, TArgs...>将导致一个函数采用rvalue引用,它不会绑定到args....一个简单的解决方法是强制它通过折叠引用来获取左值引用:
creator_ = std::bind(&std::make_shared<T,TArgs&...>, args...);
// ^
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用lambda,它更具可读性:
creator_ = [=](){return std::make_shared<T>(args...);};
Run Code Online (Sandbox Code Playgroud)