c++ - typedef 有效, 'using =' 无效

标签 c++ templates c++11 typedef using

我有一段代码,稍微简化一下,相当于以下编译和工作正常的代码。

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) , 编译器无法推断类型 InterfaceArgs , 所以调用 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/

相关文章:

c++ - 如何确定 cin 是键盘还是来自文件

c++ - 专门化采用通用引用参数的函数模板

c++ - Visual C++ 2010 原子类型支持?

c++ - 当数组的第一个元素最高时,Findmax() 方法返回第二高的值

c++ - 'for loop' 的终止条件是否在 VC++ 6 中刷新?

c++ - 根据 move 赋值运算符 move 构造函数

c++ - 我可以按类类型访问什么类型的集合,并在必要时返回找不到

C++11 "late binding"模板参数

c++ - 如何检查在编译时是否调用了模板化方法?

c++ - 部分模板绑定(bind),创建新模板作为类型