我有一段代码,稍微简化一下,相当于以下编译和工作正常的代码。
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;
}
但是当尝试使用较新的 using ... =
时在这样的结构中构造而不是 typedef:
template <typename Interface, typename... Args>
using factory_function = function<shared_ptr<Interface> (Args...)>;
然后更改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>’
更新:
这是完整的、可编译的测试用例,用 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;
}
注释行显示了不的工作。
最佳答案
与 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>
的类型到 std::function
.以下内容可能会有所帮助:
template <typename R, typename...Args>
std::function<R(Args...)> make_function(R (*f)(Args...))
{
return f;
}
之后:
register_factory<Iface>(1000, make_function(create_function<Iface, Impl>));
关于c++ - typedef 有效, 'using =' 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23853397/