如何在C++中使用带有预处理器的 gcc __is_class

How to use gcc __is_class with preprocessor if in C++

本文关键字:处理器 预处理 gcc is class C++      更新时间:2023-10-16

如果函数是一个类,我想向函数发送一个变量。我尝试使用 std::is_class但这给了我一个错误,因为 std::is_class 是运行时而不是预处理器。如果工作,我无法制作预处理器。有什么建议吗?在循环中的函数"toJson"中,我将类型添加到 json 对象中。我需要检查类型是否为类,以便递归地将类字段添加到 json 对象。

源代码: https://stackoverflow.com/a/34165367/13277578

template<typename Class, typename T>
struct PropertyImpl {
constexpr PropertyImpl(T Class::* aMember, const char* aName) : member{ aMember }, name{ aName } {}
using Type = T;
T Class::* member;
const char* name;
};
template<typename Class, typename T>
constexpr auto property(T Class::* member, const char* name) {
return PropertyImpl<Class, T>{member, name};
}
template <typename T, T... S, typename F>
constexpr void for_sequence(std::integer_sequence<T, S...>, F&& f) {
(static_cast<void>(f(std::integral_constant<T, S>{})), ...);
}
// the error in this function
template<typename T>
json toJson(const T& object)
{
json data;
// We first get the number of properties
constexpr auto nbProperties = std::tuple_size<decltype(T::properties)>::value;
// We iterate on the index sequence of size `nbProperties`
for_sequence(std::make_index_sequence<nbProperties>{}, [&](auto i) {
// get the property
constexpr auto property = std::get<i>(T::properties);
using Type = typename decltype(property)::Type;
#if __is_class(Type) // C1017
// data[property.name] = toJson(object.*(property.member))  // recursive add the object fields
#else
// addFind(data, object.*(property.member), property.name)) // add the object field (WORK)
#endif
});
return data;
}

使用 toJson 的示例:

#define PROPERTY(CLASS, MEMBER) property(&CLASS::MEMBER, #MEMBER)
struct GetRooms
{
unsigned int status = 0;
RoomData roomData;
string name;
constexpr static auto properties = std::make_tuple(
PROPERTY(GetRooms, status),
PROPERTY(GetRooms, roomData),
PROPERTY(GetRooms, name)
);
};
GetRooms var;
json varAsJson = toJson(var);

if constexpr正是我所需要的。它可以在编译时比较类型,这允许我调用正确的函数并完全忽略与此类型不兼容的其他函数。

if constexpr (std::is_class<Type>::value)
{
// call the class function
}
else
{
// not call the class function
}