从成员指针中提取它所指向的类的类型



如何使用C++20正确编译以下内容,也就是计算extract<mem_fun>::type?有可能吗?

像传递非成员函数或私有函数extract<>这样的错误场景对我来说并不那么重要

#include <concepts>
struct x
{
void f()
{
}
};
template<auto mem_fun>
struct extract
{
// using type= ???
};
int main()
{
using namespace std;
static_assert(same_as<typename extract<&x::f>::type, x>);
return 0;
}

指向成员的指针都具有类型T C::*,其中T可以是某种类型或某种函数类型,甚至是某种";可恶的";函数类型。

所以你只需要专注于特定的形状:

template<auto mem_fun>
struct extract;
template <typename T, typename C, T C::* v>
struct extract<v> {
using type = C;
};

最新更新