我可以获取成员函数模板参数的所属对象吗? [英] Can I get the Owning Object of a Member Function Template Parameter?

查看:77
本文介绍了我可以获取成员函数模板参数的所属对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个对象:

struct foo {
    void func();
};

现在给出了模板化的函数声明:

Now given the templatized function declaration:

template<typename T, T F>
void bar();

所以bar将采用这样的成员函数:

So bar will be taking in a member function like so:

bar<decltype(&foo::func), &foo::func>()

bar的主体中,我想从T恢复类型foo.我可以那样做吗?我希望能够做这样的事情:

In the body of bar I want to recover the type foo from T. Can I do that? I want to be able to do something like this:

get_obj<T> myfoo;

(myfoo.*F)();

我知道get_obj不是一回事,但是有办法写吗?

I know that get_obj isn't a thing, but would there be a way to write it?

推荐答案

template<class T>
struct get_memfun_class;
template<class R, class T, class...Args>
struct get_memfun_class<R(T::*)(Args...)> {
  using type=T;
};
template<class T>
using get_memfun_class_t=typename get_memfun_class<T>::type;


template<auto M>
using class_of_memfun = get_memfun_class_t< decltype(M) >;

class_of_memfun<F>是成员函数F的类.

要处理const/volatile/etc,您最终必须做很多版本.真烦人这是一个示例:

To handle const/volatile/etc you end up having to do a bunch of versions. This is annoying. Here is an example of it:

template<class T>
struct get_memfun_class;
#define GET_MEMFUN_CLASS(...) \
template<class R, class T, class...Args> \
struct get_memfun_class<R(T::*)(Args...) __VA_ARGS__> { \
  using type=T; \
}

可能想要:

template<class R, class T, class...Args> \
struct get_memfun_class<R(T::*)(Args...) __VA_ARGS__> { \
  using type=T __VA_ARGS__; \
}

指向const memfun的指针的类的类型是否为const类?

is the type of the class of a pointer to a const memfun a const class or not?

选择之后,您需要编写上述宏的24种用法:

Once you have chosen, you need to write 24 uses of the above macro:

GET_MEMFUN_CLASS();
GET_MEMFUN_CLASS(const);
GET_MEMFUN_CLASS(volatile);
GET_MEMFUN_CLASS(const volatile);
GET_MEMFUN_CLASS(&);
GET_MEMFUN_CLASS(const&);
GET_MEMFUN_CLASS(volatile&);
GET_MEMFUN_CLASS(const volatile&);
GET_MEMFUN_CLASS(&&);
GET_MEMFUN_CLASS(const&&);
GET_MEMFUN_CLASS(volatile&&);
GET_MEMFUN_CLASS(const volatile&&);
GET_MEMFUN_CLASS(noexcept);
GET_MEMFUN_CLASS(const noexcept);
GET_MEMFUN_CLASS(volatile noexcept);
GET_MEMFUN_CLASS(const volatile noexcept);
GET_MEMFUN_CLASS(& noexcept);
GET_MEMFUN_CLASS(const& noexcept);
GET_MEMFUN_CLASS(volatile& noexcept);
GET_MEMFUN_CLASS(const volatile& noexcept);
GET_MEMFUN_CLASS(&& noexcept);
GET_MEMFUN_CLASS(const&& noexcept);
GET_MEMFUN_CLASS(volatile&& noexcept);
GET_MEMFUN_CLASS(const volatile&& noexcept);
#undef GET_MEMFUN_CLASS

template<class T>
using get_memfun_class_t=typename get_memfun_class<T>::type;

我不知道如何避免对所有这24个专业进行全部覆盖.如果您认为这很愚蠢,那您是对的;请随时提出建议,向C ++标准委员会提出解决方案.

I am unaware of a way to avoid doing all 24 of these specializations for full coverage. If you think this is dumb, you are right; please feel free to express your annoyance by proposing a fix to the C++ standard committee.

如果您要针对多个特征进行此类操作,则可以将"strip lvalue,rvalue,noexcept和cv限定词"分开写成一个部分,然后逐个传递.

If you are doing something like this for more than one trait, you can write the "strip lvalue, rvalue, noexcept and cv qualifiers" off part at one spot and pass them down in pieces.

实时示例.

这篇关于我可以获取成员函数模板参数的所属对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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