重载成员函数的decltype [英] decltype for overloaded member function

查看:168
本文介绍了重载成员函数的decltype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

struct Foo 
{
    int print(int a, double b);
    int print(int a);
    void print();
    void print(int a, int b, int c);

    void other();
};

我可以打电话

decltype(&Foo::other)

但正在呼叫

decltype(&Foo::print)

以错误结束,这对我来说很清楚。

end with error, which is clear to me.

但是我如何更紧密地指定四个<$ c $中的哪个c> print 方法,我想解析为 decltype 吗?

But how can I specify more "closely" which of the four print methods, I want to resolve to decltype?

我想在

template <class MT>
struct method_info;

template <class T, class Res, class... Args>
struct method_info<Res(T::*)(Args...)>
{
    typedef std::tuple<Args&&...> args_tuple;
    typedef T ClassType;
    typedef Res RetVal; 
};



template <class MethodType>
void func() {
   typedef method_info<MethodType> MethodInfo;
   .....
}

func<decltype(&Foo::other)>();
....


推荐答案

更多据我了解,表示您要指定 print 的函数参数。也就是说,例如,选择 int,int ,然后取回 Foo {}。result-type的结果类型。 {}),然后从所有可用信息中构造一个函数指针。

More "closely", as far as I understand, means you want to specify the function arguments of print. That is, for example, you select int, int, then get back the result-type of Foo{}.print(int{},int{}), and thereafter construct a function pointer from all the available information.

这里是一个别名模板,可以在一般方式:

Here is an alias template which does that for you in a general way:

template<typename ... Args>
using ptr_to_print_type = decltype(std::declval<Foo>().print(std::declval<Args>() ...)) (Foo::*)(Args ...);

您还可以使用 std :: result_of 而不是 std :: declval 之类的东西,但我更喜欢后者。

You could also use std::result_of instead of the std::declval stuff, but I like the latter more.

您可以将以上内容用作

func<ptr_to_print_type<int,int> >();






编辑:按@JavaLover的要求,顺便说一句,对于这种可怕的C ++屎来说,这似乎是不合适的名称:-),这与上面使用 std :: result_of unested 的测试方法相同)和错误):


as asked for by @JavaLover, which btw seems like an unsuitable name for such horrible C++ shit :-), here is the same as above using std::result_of (untested now tested and wrong):

//------ does not compile for overloaded functions --------
template<typename ... Args>
using ptr_to_print_type = std::result_of_t<decltype(&Foo::print)(Foo, Args ...)> (Foo::*)(Args ...)
//------ does not compile for overloaded functions --------

您可以进一步提取 Foo 而不是打印(除非您使用宏)。

You could further abstract away the Foo but not the print (unless you're using macros).

这篇关于重载成员函数的decltype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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