这是我使用 C++17 的版本,使用模板时不需要decltype(更方便一点):
#include <iostream>
#include <string>
#include <type_traits>
template <auto Fp>
struct field_type;
template<typename R, typename T, R (T::*FP)>
struct field_type<FP>
{
using type = R;
};
template<typename R, typename T, typename...Args, R (T::*FP)(Args...)>
struct field_type<FP>
{
using type = R;
};
template<auto FP>
using field_type_t = typename field_type<FP>::type;
class Foo {
public:
int x = 0;
double y = 0;
std::string s;
const int cx = 0;
Foo() = default;
void bar() {
std::cout << "bar\n";
}
int par(int z) {
std::cout << "bar\n";
return z;
}
};
template<auto F, typename T>
constexpr bool test = std::is_same_v<field_type_t<F>, T>;
static_assert(test<&Foo::x, int>, "");
static_assert(test<&Foo::cx, const int>, "");
static_assert(test<&Foo::s, std::string>, "");
static_assert(test<&Foo::y, double>, "");
#ifndef HIDE_PROBLEM_ON_GCC_11
static_assert(test<&Foo::bar, void>, "");
static_assert(test<&Foo::par, int>, "");
#endif
出于某种奇怪的原因,它适用于所有编译器,但不适用于 gcc 11.1 或更高版本。
https://godbolt.org/z/4e7oKbod1