Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

c++ - Can I get the Owning Object of a Member Function Template Parameter?

Given a object:

struct foo {
    void func();
};

Now given the templatized function declaration:

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

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

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

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)();

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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> is then the class of the memberfunction F.

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; 
}

possibly you want:

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

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

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;

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.

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.

Live example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...