Ele*_*ito 4 c++ templates typedef using c++11
我有一段代码,简化了一点,相当于以下编译和正常工作.
template <typename Interface, typename... Args>
struct factory_function {
typedef function<shared_ptr<Interface> (Args...)> type;
};
template <typename Interface, typename Implementer, typename... Args>
shared_ptr<Interface> create_function(Args... args) {
return make_shared<Implementer>(args...);
}
template <typename Interface, typename... Args>
int register_factory(identifier id, typename factory_function<Interface, Args...>::type factory) {
}
int main(int argc, char *argv[]) {
register_factory<Iface>(1000, create_function<Iface, Impl>);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当尝试using ... =在这样的结构中使用较新的构造而不是typedef时:
template <typename Interface, typename... Args>
using factory_function = function<shared_ptr<Interface> (Args...)>;
Run Code Online (Sandbox Code Playgroud)
然后typename factory_function<Interface, Args...>::type改成factory_function<Interface, Args...>,我得到一个编译错误:
foo.cc: In function ‘int main(int, char**)’:
foo.cc:31:61: error: no matching function for call to ‘register_factory(int, <unresolved overloaded function type>)’
register_factory<Iface>(1000, create_function<Iface, Impl>);
^
foo.cc:31:61: note: candidate is:
foo.cc:17:5: note: template<class Interface, class ... Args> int register_factory(identifier, factory_function<Interface, Args ...>)
int register_factory(identifier id, factory_function<Interface, Args...> factory) {
^
foo.cc:17:5: note: template argument deduction/substitution failed:
foo.cc:31:61: note: mismatched types ‘std::function<std::shared_ptr<Iface>(Args ...)>’ and ‘std::shared_ptr<Iface> (*)()’
register_factory<Iface>(1000, create_function<Iface, Impl>);
^
foo.cc:31:61: note: could not resolve address from overloaded function ‘create_function<Iface, Impl>’
Run Code Online (Sandbox Code Playgroud)
更新:
这是完整的,可编译的测试用例,编译有g++ -std=c++11 foo.cc:
#include <functional>
#include <memory>
using namespace std;
typedef int identifier;
template <typename Interface, typename... Args>
struct factory_function {
typedef function<shared_ptr<Interface> (Args...)> type;
};
//template <typename Interface, typename... Args>
//using factory_function = function<shared_ptr<Interface> (Args...)>;
template <typename Interface, typename Implementer, typename... Args>
shared_ptr<Interface> create_function(Args... args) {
return make_shared<Implementer>(args...);
}
template <typename Interface, typename... Args>
int register_factory(identifier id, typename factory_function<Interface, Args...>::type factory) {
//int register_factory(identifier id, factory_function<Interface, Args...> factory) {
}
class Iface {
public:
virtual void foo() = 0;
};
class Impl : public Iface {
public:
virtual void foo() {}
};
int main(int argc, char *argv[]) {
register_factory<Iface>(1000, create_function<Iface, Impl>);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注释行显示什么是不工作的.
随着int register_factory(identifier id, typename factory_function<Interface, Args...>::type factory),编译器不能推断类型Interface和Args,所以调用register_factory<Iface>(1000, create_function<Iface, Impl>);是明确int register_factory(identifier id, typename factory_function<Interface>::type);
使用替代定义(使用)int register_factory(identifier id, factory_function<Interface, Args...>,编译器必须尝试推导Args(Interface显式设置为Iface)但不幸的是,需要转换并且编译器失败(std::shared_ptr<Iface> (*)()不是任何完全匹配function<std::shared_ptr<Iface>(Args...)>)
解决方法是你的第二个解决方案是强制类型create_function<Iface, Impl>为a std::function.以下内容可能有所帮助:
template <typename R, typename...Args>
std::function<R(Args...)> make_function(R (*f)(Args...))
{
return f;
}
Run Code Online (Sandbox Code Playgroud)
然后:
register_factory<Iface>(1000, make_function(create_function<Iface, Impl>));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |