我需要根据类成员的类型进行一些编译时代码分析。 我找到了一个很好的libabry(magic_get),允许迭代类实例的成员。我需要做类似的事情,但仅限于类型(以便它在编译时工作)
#include <iostream>
#include <boost/pfr/flat.hpp>
#include <type_traits>
struct PtrA { char *s; int a; };
struct NoPtrB{ int b; };
struct C { int c; PtrA a; NoPtrB b; };
template<typename T>
constexpr bool has_pointer(T&&var)
{
bool ret = false;
boost::pfr::flat_for_each_field(var,
[&ret](auto& field) noexcept
{
if( std::is_pointer<typename std::decay<decltype(field)>::type>::value)
ret = true;
}
);
return ret;
}
int main()
{
//static_assert(has_pointer<C>(),"has_pointer"); // <-- I'd like to have working at compile time
std:: cout << "Has pointer = "<< has_pointer(C()) << std::endl;
std:: cout << "Has pointer = "<< has_pointer(NoPtrB()) << std::endl;
}