I have a pointer to the method:
struct A { int method() { return 0; } };
auto fn = &A::method;
I can get a return type by std::result_of, but how I can get from fn the class owner of the method?
I have a pointer to the method:
struct A { int method() { return 0; } };
auto fn = &A::method;
I can get a return type by std::result_of, but how I can get from fn the class owner of the method?
Try this:
template<class T>
struct MethodInfo;
template<class C, class R, class... A>
struct MethodInfo<R(C::*)(A...)> //method pointer
{
typedef C ClassType;
typedef R ReturnType;
typedef std::tuple<A...> ArgsTuple;
};
template<class C, class R, class... A>
struct MethodInfo<R(C::*)(A...) const> : MethodInfo<R(C::*)(A...)> {}; //const method pointer
template<class C, class R, class... A>
struct MethodInfo<R(C::*)(A...) volatile> : MethodInfo<R(C::*)(A...)> {}; //volatile method pointer
const volatile
. –
Cockalorum You can match it using class-template-specialization:
//Primary template
template<typename T> struct ClassOf {};
//Thanks T.C for suggesting leaving out the funtion /^argument
template<typename Return, typename Class>
struct ClassOf<Return (Class::*)>{ using type = Class; };
//An alias
template< typename T> using ClassOf_t = typename ClassOf<T>::type;
Hence given:
struct A { int method() { return 0; } };
auto fn = &A::method;
We can retrieve the class like:
ClassOf_t<decltype(fn)> a;
Full example Here.
F C::*
instead of having 48 specializations for every possible function type. –
Pee Try this:
template<class T>
struct MethodInfo;
template<class C, class R, class... A>
struct MethodInfo<R(C::*)(A...)> //method pointer
{
typedef C ClassType;
typedef R ReturnType;
typedef std::tuple<A...> ArgsTuple;
};
template<class C, class R, class... A>
struct MethodInfo<R(C::*)(A...) const> : MethodInfo<R(C::*)(A...)> {}; //const method pointer
template<class C, class R, class... A>
struct MethodInfo<R(C::*)(A...) volatile> : MethodInfo<R(C::*)(A...)> {}; //volatile method pointer
const volatile
. –
Cockalorum Boost callable traits answer, I like it better than shorter answers here since it is a bit more readable to me, but opinions might differ...
#include<string>
#include<type_traits>
#include<tuple>
#include <boost/callable_traits/args.hpp>
struct S{
int val=46;
int get(){
return val;
}
void method(const std::string ){
}
};
int main(){
using Ts1 = boost::callable_traits::args_t<decltype(&S::val)>;
using Ts2 = boost::callable_traits::args_t<decltype(&S::get)>;
using Ts3 = boost::callable_traits::args_t<decltype(&S::method)>;
std::remove_cvref_t<std::tuple_element<0,Ts1>::type> s1;
s1.val++;
std::remove_cvref_t<std::tuple_element<0,Ts2>::type> s2;
s2.val++;
std::remove_cvref_t<std::tuple_element<0,Ts3>::type> s3;
s3.val++;
}
s1
, s2
, s3
are all of type S.
Obviously you need to do the logic only once, I did it 3 times to show it works fine for pointer to member, pointer to function that takes 0 arguments, pointer to function that takes 1 argument.
© 2022 - 2024 — McMap. All rights reserved.