(不要为使用std::auto_ptr 而烦恼,这不是我的代码,它是自动生成的。我只是在尝试与其连接。)我有一个带有以下签名的函数:std::auto_ptr<T> gFunction(const std::string&, int, const int&)此功能已被重载了588次,因此将其分配给boost::function是模棱两可的。好的,我将重铸它,然后分配它。键入以下内容比较麻烦:std::auto_ptr<T> (*func)(const std::string&, int, const int&) = &gFunction我想对此进行typedef编码,因此可以按以下方式使用它:function_type func = &gFunction所以我尝试typedeftypedef std::auto_ptr<T>(*funct)(const std::string&, int, const int&) function_type;但是我的typedef返回了error: expected ‘;’ before ‘function_type’有任何想法吗?我可能缺少一些简单的东西。 最佳答案 就是这样:typedef std::auto_ptr<T>(*function_type)(const std::string&, int, const int&);还请记住,自C++ 11起不推荐使用std::auto_ptr。如果可以的话,应该改用std::unique_ptr。在C++ 11中,您还可以使用 std::add_pointer 类型特征来添加指向函数类型的指针(如果这样会使您更直观):#include <type_traits>typedef typename std::add_pointer< std::shared_ptr<T>(const std::string&, int, const int&) >::type function_type;同样,as mentioned by Mike Seymour in the comments,您可以考虑将类型别名function_type定义为函数类型(顾名思义),而不是使其成为函数指针类型(只需删除*即可)。关于c++ - Typedef期望 ';'之前为 “”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16699655/
10-16 20:41