1
4

2 回答 2

4

Just do it this way:

typedef std::auto_ptr<T>(*function_type)(const std::string&, int, const int&);

Also keep in mind, that std::auto_ptr is deprecated since C++11. If you can, you should use std::unique_ptr instead.

In C++11 you can also use the std::add_pointer type trait to add a pointer to a function type (if that makes it more intuitive to you):

#include <type_traits>

typedef typename std::add_pointer<
    std::shared_ptr<T>(const std::string&, int, const int&)
    >::type function_type;

Also, as mentioned by Mike Seymour in the comments, you may consider defining the type alias function_type as a function type (as the name would suggest), rather than making it a function pointer type (just drop the * to do that).

于 2013-05-22T19:02:14.193 回答
0

The proper syntax for declaring a typedef should mimic a variable declaration. Syntactically, they are the same except you prefix it with 'typedef'. Semantically the declared names form very different purposes(one makes a variable, one makes a type)

just as you typed the following to declared a variable

std::auto_ptr<T> (*func)(const std::string&, int, const int&);

you can make this a typedef just be adding a "typedef" to the front

typedef std::auto_ptr<T> (*func)(const std::string&, int, const int&);
于 2013-05-22T19:08:20.990 回答