#include
#include
#include
struct Point {
double x;
double y;
};
struct AnyType {
template
operator T();
};
template
consteval size_t CountMember(auto&&... Args) {
if constexpr (! requires { T{ Args... }; }) { // (1)
return sizeof...(Args) - 1;
} else {
return CountMember(Args..., AnyType{}); // (2)
}
}
int main(int argc, char** argv) {
struct Test { int a; int b; int c; double d; };
printf("Test:%zu
", CountMember());
printf("Point:%zu
", CountMember());
}
运行结果:
Test:4
Point:2
在线测试代码
https://wandbox.org/
#include
#include
#include
template< class... >
using void_t = void;
struct Point {
double x;
double y;
};
struct AnyType {
template
operator T();
};
template
struct CountMember {
constexpr static size_t value = sizeof...(Ts) - 1;
};
template
struct CountMember, Ts...> {
constexpr static size_t value = CountMember::value;
};
int main(int argc, char** argv) {
struct Test { int a; int b; int c; double d; };
printf("Test:%zu
", CountMember::value);
printf("Point:%zu
", CountMember::value);
}
留言与评论(共有 0 条评论) “” |