可以使用C ++ 11 decltype从现有函数为函数指针创建typedef吗? [英] Can C++11 decltype be used to create a typedef for function pointer from an existing function?

查看:71
本文介绍了可以使用C ++ 11 decltype从现有函数为函数指针创建typedef吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出

struct A { 
    int foo(double a, std::string& b) const;
};

我可以创建这样的成员函数指针:

I can create a member function pointer like this:

typedef int (A::*PFN_FOO)(double, std::string&) const;

很容易,除了 PFN_FOO 需要如果 A :: foo 的签名更改,则将更新。由于C ++ 11引入了 decltype ,它可以用于自动推断签名并创建typedef吗?

Easy enough, except that PFN_FOO needs to be updated if A::foo's signature changes. Since C++11 introduces decltype, could it be used to automatically deduce the signature and create the typedef?

推荐答案

是的,当然:

typedef decltype(& A :: foo)PFN_FOO; code>

您也可以通过 使用 关键字(感谢Matthieu M。):

You can also define type alias via using keyword (Thanks to Matthieu M.):

using PFN_FOO = decltype(& A :: foo);

这篇关于可以使用C ++ 11 decltype从现有函数为函数指针创建typedef吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆