c++ - 如何在 Visual Studio 2013 中为函数类型创建类型别名?

标签 c++ c++11 visual-studio-2013

使用 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/

相关文章:

c++ - 你如何为 x64 编译 OpenSSL?

c++ - 如何在 C++11 中访问 double 的 pair<pair<string,string>,double>

authentication - 配置 Visual Studio MVC 身份验证时用户凭据验证失败

c++ - QCustomPlot 中的 QImage/Logo

c++ - 在文本中搜索 25 000 个单词

c++ - Utf-8 编码 visual studios 十进制值

c++ - 具有数组构造函数方法的 constexpr 类

asp.net-mvc - MVC 5 上的成员资格提供程序是什么?

c# - 为什么在 C# 中启用多行的 TextBox 控件上禁用 CTRL+A 快捷方式?

C++ 派生多态类 - 它是否包含 Base 的整个实例,包括 vptr?