C++ 按成员函数指针值类别的模板专门化
我想将成员函数指针的类型传递给类模板,以便模板可以使用参数包处理函数参数类型。例如,我想计算成员函数的参数类型大小之和。为了使其适用于所有值类别,我需要为不同的函数指针类型提供24个(我忘记了吗?)专门化:C++ 按成员函数指针值类别的模板专门化,c++,templates,metaprogramming,C++,Templates,Metaprogramming,我想将成员函数指针的类型传递给类模板,以便模板可以使用参数包处理函数参数类型。例如,我想计算成员函数的参数类型大小之和。为了使其适用于所有值类别,我需要为不同的函数指针类型提供24个(我忘记了吗?)专门化: #include <iostream> struct X { void foo (char) {} void cfoo (short) const {} void vfoo (short, char) volatile {} void cvfoo
#include <iostream>
struct X {
void foo (char) {}
void cfoo (short) const {}
void vfoo (short, char) volatile {}
void cvfoo (int) const volatile {}
void lfoo (int, char) & {}
void clfoo (int, short) const & {}
void rfoo (int, short, char) && {}
void crfoo (int, int) const && {}
void vlfoo (int, int, char) volatile & {}
void cvlfoo (int, int, short) const volatile & {}
void vrfoo (int, int, short, char) volatile && {}
void cvrfoo (int, int, int) const volatile && {}
void vafoo (char, ...) {}
void vacfoo (short, ...) const {}
void vavfoo (short, char, ...) volatile {}
void vacvfoo (int, ...) const volatile {}
void valfoo (int, char, ...) & {}
void vaclfoo (int, short, ...) const & {}
void varfoo (int, short, char, ...) && {}
void vacrfoo (int, int, ...) const && {}
void vavlfoo (int, int, char, ...) volatile & {}
void vacvlfoo (int, int, short, ...) const volatile & {}
void vavrfoo (int, int, short, char, ...) volatile && {}
void vacvrfoo (int, int, int, ...) const volatile && {}
};
template <typename TPtr, TPtr Ptr>
struct ArgsSize;
#define DEF_ArgSize_SPEC(qual) template <typename R, typename C, typename... Args, R (C::*fPtr) (Args...) qual> \
struct ArgsSize<R (C::*) (Args...) qual, fPtr> { \
static constexpr std::size_t value = (0 + ... + sizeof(Args)); \
}; \
template <typename R, typename C, typename... Args, R (C::*fPtr) (Args..., ...) qual> \
struct ArgsSize<R (C::*) (Args..., ...) qual, fPtr> { \
static constexpr std::size_t value = 1000 + (0 + ... + sizeof(Args)); /* For testing, add a big number to denote variadic arguments */ \
};
DEF_ArgSize_SPEC(&)
DEF_ArgSize_SPEC(const &)
DEF_ArgSize_SPEC(&&)
DEF_ArgSize_SPEC(const &&)
DEF_ArgSize_SPEC(volatile &)
DEF_ArgSize_SPEC(const volatile &)
DEF_ArgSize_SPEC(volatile &&)
DEF_ArgSize_SPEC(const volatile &&)
DEF_ArgSize_SPEC()
DEF_ArgSize_SPEC(const)
DEF_ArgSize_SPEC(volatile)
DEF_ArgSize_SPEC(const volatile)
template <auto Ptr>
void test () {
std::cout << ArgsSize<decltype(Ptr), Ptr>::value << std::endl;
}
int main () {
test <&X::foo> ();
test <&X::cfoo> ();
test <&X::vfoo> ();
test <&X::cvfoo> ();
test <&X::lfoo> ();
test <&X::clfoo> ();
test <&X::rfoo> ();
test <&X::crfoo> ();
test <&X::vlfoo> ();
test <&X::cvlfoo> ();
test <&X::vrfoo> ();
test <&X::cvrfoo> ();
test <&X::vafoo> ();
test <&X::vacfoo> ();
test <&X::vavfoo> ();
test <&X::vacvfoo> ();
test <&X::valfoo> ();
test <&X::vaclfoo> ();
test <&X::varfoo> ();
test <&X::vacrfoo> ();
test <&X::vavlfoo> ();
test <&X::vacvlfoo> ();
test <&X::vavrfoo> ();
test <&X::vacvrfoo> ();
}
#包括
结构X{
void foo(char){}
void cfoo(短)常量{}
void vfoo(短,字符)volatile{}
void cvfoo(int)const volatile{}
void lfoo(int,char)&{}
void clfoo(int,short)const&{}
void rfoo(int,short,char)&&{}
void crfoo(int,int)const&&{}
void vlfoo(int,int,char)volatile&{}
void cvlfoo(int,int,short)常量volatile&{}
void vrfoo(int,int,short,char)volatile&&{}
void cvrfoo(int,int,int)常量volatile&&{}
void vafoo(char,…{}
void vacfoo(短,…)const{}
void vavfoo(短,字符,…)volatile{}
void vacvfoo(int,…)常量volatile{}
void valfoo(int,char,…)和{}
void vaclfoo(int,short,…)const&{}
void varfoo(int,short,char,…)和&{}
void vacrfo(int,int,…)const&&{}
void-vavlfoo(int,int,char,…)volatile&{}
void vacvloo(int,int,short,…)const volatile&{}
void vavrfoo(int,int,short,char,…)volatile&&{}
void vacvrfoo(int,int,int,…)常量volatile&&{}
};
模板
结构参数化;
#定义定义参数大小规格(质量)模板\
结构ArgsSize{\
静态constepr std::size_t value=(0+…+sizeof(Args))\
}; \
模板\
结构ArgsSize{\
static constexpr std::size_t value=1000+(0+…+sizeof(Args));/*对于测试,请添加一个大数字以表示可变参数*/\
};
DEF_ArgSize_规格(&)
定义参数规格(常数和)
DEF_ArgSize_规格(&&)
定义参数规格(常数和)
DEF_ArgSize_规格(易失性&)
DEF_ArgSize_规格(常量易失性&)
DEF_ArgSize_规格(易失性&&)
DEF_ArgSize_规格(常量挥发性&&)
DEF_ArgSize_SPEC()
DEF_ArgSize_规格(常数)
DEF_ArgSize_规格(挥发性)
DEF_ArgSize_规格(常数可变)
模板
空隙试验(){
std::你会不会错过变量为Ret(C::*)(Args…
(没有&
,&
)和C-省略号(Ret(C:*)(Args…
为printf
)。哦,对了!我认为没有任何东西和“&”的意思是一样的。现在看起来更糟了:)我会说创建(或使用一些库)要让函数特性有一次样板,请使用类似于(method\u traits::Args+…)+1000*method\u traits::has\u省略号+42*method\u traits::is\u const
。