C++:从结构中的静态函数中提取参数类型
C++: Extracting parameter type from static function in struct
假设我有这样的东西:
template<typename T, typename R>
struct MyStruct {
static R myfunc(T);
};
struct MyStructInst: S<int, double> {
static double myfunc(int i) { return i; }
};
然后,我有一个采用 M
的模板,我假设这种类型具有一个带一个参数的静态 myfunc
函数(例如 MyStructInst
)。我想提取myfunc
:
的参数类型和结果类型
template<typename M,
typename ParamType = ???,
typename ResultType = decltype(declval(M::myfunc))> // I think this works?
struct MyStruct2 {
...
};
获得 ParamType
的最简单方法是什么?
怎么样
template <typename R, typename A0, typename ... As>
constexpr A0 firstArg (R(*)(A0, As...));
template <typename M,
typename PT = decltype(firstArg(&M::myfunc)),
typename RT = decltype(M::myfunc(std::declval<PT>()))>
struct MyStruct2
{ };
?
我的意思是...如果你声明(没有必要实现它,因为它只在 decltype()
内部使用,所以只有 returned 类型很重要)接收的函数接收一个或多个参数和 return 第一个参数类型的函数类型(我记得指向 static 方法的指针就像指向传统函数的指针)
template <typename R, typename A0, typename ... As>
constexpr A0 firstArg (R(*)(A0, As...));
给定一个模板类型名M
,你可以用
获得myFunc()
方法的第一个参数(如果有的话)
typename PT = decltype(firstArg(&M::myfunc))
给定 PT
(第一个类型参数的类型),您可以获得 returned 类型,只需模拟(在 decltype()
内,使用 std::declval()
)调用使用 PT
类型的对象 myfunc()
typename RT = decltype(M::myfunc(std::declval<PT>()))
下面是一个完整的编译示例
#include <string>
#include <type_traits>
template <typename T, typename R>
struct MyStruct
{ static R myfunc(T); };
struct MyStructInst
{ static double myfunc(int i) { return i; } };
template <typename R, typename A0, typename ... As>
constexpr A0 firstArg (R(*)(A0, As...));
template <typename M,
typename PT = decltype(firstArg(&M::myfunc)),
typename RT = decltype(M::myfunc(std::declval<PT>()))>
struct MyStruct2
{ using type1 = PT; using type2 = RT; };
int main ()
{
static_assert( std::is_same<int,
typename MyStruct2<MyStructInst>::type1>{}, "!");
static_assert( std::is_same<double,
typename MyStruct2<MyStructInst>::type2>{}, "!");
static_assert( std::is_same<long,
typename MyStruct2<MyStruct<long, std::string>>::type1>{}, "!");
static_assert( std::is_same<std::string,
typename MyStruct2<MyStruct<long, std::string>>::type2>{}, "!");
}
假设我有这样的东西:
template<typename T, typename R>
struct MyStruct {
static R myfunc(T);
};
struct MyStructInst: S<int, double> {
static double myfunc(int i) { return i; }
};
然后,我有一个采用 M
的模板,我假设这种类型具有一个带一个参数的静态 myfunc
函数(例如 MyStructInst
)。我想提取myfunc
:
template<typename M,
typename ParamType = ???,
typename ResultType = decltype(declval(M::myfunc))> // I think this works?
struct MyStruct2 {
...
};
获得 ParamType
的最简单方法是什么?
怎么样
template <typename R, typename A0, typename ... As>
constexpr A0 firstArg (R(*)(A0, As...));
template <typename M,
typename PT = decltype(firstArg(&M::myfunc)),
typename RT = decltype(M::myfunc(std::declval<PT>()))>
struct MyStruct2
{ };
?
我的意思是...如果你声明(没有必要实现它,因为它只在 decltype()
内部使用,所以只有 returned 类型很重要)接收的函数接收一个或多个参数和 return 第一个参数类型的函数类型(我记得指向 static 方法的指针就像指向传统函数的指针)
template <typename R, typename A0, typename ... As>
constexpr A0 firstArg (R(*)(A0, As...));
给定一个模板类型名M
,你可以用
myFunc()
方法的第一个参数(如果有的话)
typename PT = decltype(firstArg(&M::myfunc))
给定 PT
(第一个类型参数的类型),您可以获得 returned 类型,只需模拟(在 decltype()
内,使用 std::declval()
)调用使用 PT
myfunc()
typename RT = decltype(M::myfunc(std::declval<PT>()))
下面是一个完整的编译示例
#include <string>
#include <type_traits>
template <typename T, typename R>
struct MyStruct
{ static R myfunc(T); };
struct MyStructInst
{ static double myfunc(int i) { return i; } };
template <typename R, typename A0, typename ... As>
constexpr A0 firstArg (R(*)(A0, As...));
template <typename M,
typename PT = decltype(firstArg(&M::myfunc)),
typename RT = decltype(M::myfunc(std::declval<PT>()))>
struct MyStruct2
{ using type1 = PT; using type2 = RT; };
int main ()
{
static_assert( std::is_same<int,
typename MyStruct2<MyStructInst>::type1>{}, "!");
static_assert( std::is_same<double,
typename MyStruct2<MyStructInst>::type2>{}, "!");
static_assert( std::is_same<long,
typename MyStruct2<MyStruct<long, std::string>>::type1>{}, "!");
static_assert( std::is_same<std::string,
typename MyStruct2<MyStruct<long, std::string>>::type2>{}, "!");
}