使用 VS2013,我可以为函数类型创建一个 typedef,如下所示:
typedef void ResponseCallback(std::string const&);
是否可以使用类型别名(我可以访问 C++11 功能)来做同样的事情?我一直在尝试放弃使用 typedef
,因为 using
似乎会更加一致。我试过类似下面的方法,但它不起作用:
using ResponseCallback = void (std::string const&);
我从 Visual Studio 2013 收到一条含糊不清的错误消息,如下所示:
error C2061: syntax error : identifier 'string'
最佳答案
但是你可以把它包起来。
template < typename P1 >
using ResponseCallback =
typename std::remove_pointer < void (*)( P1 const & ) >::type;
我在 VS2013 上测试了它,这里是 coliru
或者像这样的简单伪造包装器也能满足 VS2013 的要求:
template < typename functype >
struct functype_wrapper
{
typedef functype type;
};
//using ResponseCallback = void ( std::string const & ); // nope
using ResponseCallback = functype_wrapper < void ( std::string const & ) >::type; // oke
关于c++ - 如何在 Visual Studio 2013 中为函数类型创建类型别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26349216/