English 中文(简体)
如何获得成员职能协调人的返回类型[复制]
原标题:how to get a return type of a member function pointer [duplicate]

Is there a way to determine a return type of a member function pointer?

法典样本:

///// my library
void my_func(auto mptr) { // have to use `auto`
  // some logic based on a return type of mptr: int, string, A, etc.
}

///// client code
struct A {
  int foo();
  std::string bar(int);
};

class B{
public:
  A func(int, double);
};
// ... and many other classes

my_func(&A::foo);
my_func(&A::bar);
my_func(&B::func);
// ... many other calls of my_func()

我需要“填入”<代码>my_func()。

Edit: I can t use std::result_of/std::invoke_result as I don t know the full list of parameters of mptr. It s not important with which params a method is supposed to be called as I m not calling it. I would like to avoid creating an object of base class of mptr even if I m able to determine it (using declval is ok).

最佳回答

您可使用部分模板专业确定<代码>mptr的返回类型:

template <typename T>
struct ReturnType;

template <typename Object, typename Return, typename... Args>
struct ReturnType<Return (Object::*)(Args...)>
{
    using Type = Return;
};

void my_func(auto mptr) {
  typename ReturnType<decltype(mptr)>::Type obj;
}

http://coliru.stacked-crooked.com/a/e7761cff81061d3d”rel=“noretinger”>Live Demo

问题回答

You can write a function that deduces the type of a member function pointer, and returns the deduced return type. Note that only a declaration, and no definition is needed

template <typename C, typename Ret, typename... Args>
auto ret_type(Ret (C::*)(Args...)) -> Ret;

void my_func(auto mptr) 
{ 
  using type = decltype(ret_type(mptr));
}

我认为,这比专业化解决办法更容易阅读。

参看

也可以通过增加超载荷来核算cv-qualifiers。 e.g.

template <typename C, typename Ret, typename... Args>
auto ret_type(Ret (C::*)(Args...) const) -> Ret;

参看demo

这里,我所谈到的情况似乎大多在工作。

template <typename TYPE, typename FUNCPTR, typename...ARGS>
using mfunc_return_t = decltype((std::declval<TYPE>().*std::declval<FUNCPTR>())(std::declval<ARGS&>()...));

Then I use it in a function which calls a provided member function.

template <typename FUNCPTR, typename TYPE, typename...ARGS>
mfunc_return_t<TYPE, FUNCPTR, ARGS...> SafeCall(TYPE* ptr, FUNCPTR func, ARGS&...args)
{
    if (ptr != nullptr)
        return (ptr->*func)(args...);
    using ReturnType = mfunc_return_t<TYPE, FUNCPTR, ARGS...>;
    return DefaultValue<ReturnType>();
}

我对处理类型,包括算术类型、点工、真空和其他可缺省建筑类型,有几处缺陷。

Then I can do things like

return SafeCall(pointer, &MyClass::FunctionName, arg1, arg2);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签