如何检测给定名称的成员变量的存在和类型? [英] How to detect the presence and type of a member variable given its name?
问题描述
我知道如何编写一个类,该类可以在编译时检测给定的类T是否具有具有给定类型的给定名称的成员,例如
I know how to write a class that can detect at compile time if a given class T has a member with a given name with given type Type, e.g.
#include <type_traits>
template <typename T, typename Type, bool = std::is_class<T>::value>
struct has_member_foo
{
private:
template <Type T::*> struct helper;
template <typename U>
static std::false_type test(...);
template <typename U>
static std::true_type test(helper<&U::foo> *);
typedef decltype(test<T>(nullptr)) testresult;
public:
static const bool value = testresult::value;
};
template <typename T, typename Type>
struct has_member_foo<T, Type, false> : std::false_type { };
struct Has_Foo
{
int foo;
};
struct Has_No_Foo
{
int bar;
};
void test()
{
static_assert(has_member_foo<Has_Foo, int>::value == true, ":(");
static_assert(has_member_foo<Has_No_Foo, int>::value == false, ":(");
static_assert(has_member_foo<int, int>::value == false, ":(");
}
我不喜欢您必须声明成员变量的确切类型,因为如果我大部分时间都想使用这些特征,那么我会关心该成员是否可以转换为特定类型,是整数类型,等等..而不是确切的类型.我希望能够检测具有给定名称的成员变量的存在和类型.我希望能够编写如下内容:
I don't like that you have to state the exact type of the member variable because if I want to use these traits most of the time I care whether this member is convertible to a certain type, is an integral type, etc. and not about the exact type. I'd like to have the ability to detect the presence and the type of a member variable with a given name. I would like to be able to write something like this:
static_assert(has_member_foo<T>::value && std::is_integral<typename has_member_foo<T>::type>::value,
"The type has to have an integral member with name foo");
如果我知道& T :: foo构造是合法的,则可以通过类似的方式获取成员的类型
If I know that the construct &T::foo is legal, it is possible to get the type of the member via something like
template <typename T, typename U>
T get_member_type(T U::*);
typedef decltype(get_member_type(&T::foo)) the_member_type;
但是我不能产生SFINAE可以得到正确结果的两种方法的组合,这主要是由于helper-struct必须知道指向该成员的指针的签名.最终代码将是一个预处理器宏,名称为参数,因此任何解决方案都可以使用预处理器.
but I cannot produce a combination of the two methods that SFINAEs to the correct results, mainly due to the helper-struct has to know the signature of the pointer to the member. The final code will be a preprocessor macro with the name as argument so any solutions are allowed to use the preprocessor, too.
推荐答案
这是对名为 id
的变量进行操作的简单方法:
This is a simple way of doing it for a variable named id
:
#include <type_traits>
using namespace std;
template<typename T, typename V = bool>
struct has_id : false_type { };
template<typename T>
struct has_id<T,
typename enable_if<
!is_same<decltype(declval<T>().id), void>::value,
bool
>::type
> : true_type
{
typedef decltype(declval<T>().id) type;
};
这是您将如何使用它:
#include <iostream>
using namespace std;
struct X { int id; };
int main()
{
static_assert(
has_id<X>::value && is_integral<has_id<X>::type>::value,
"Error!"
);
}
如果您可以容忍宏,则可以使事情变得更简单:
You can make things even simpler if you can tolerate macros:
#define DEFINE_MEMBER_CHECKER(member) \
template<typename T, typename V = bool> \
struct has_ ## member : false_type { }; \
template<typename T> \
struct has_ ## member<T, \
typename enable_if< \
!is_same<decltype(declval<T>().member), void>::value, \
bool \
>::type \
> : true_type \
{ \
typedef decltype(declval<T>().member) type; \
};
#define HAS_MEMBER(C, member) \
has_ ## member<C>::value
#define MEMBER_TYPE(C, member) \
has_ ## member<C>::type
然后您可以通过以下方式使用它们:
You could then use them this way:
DEFINE_MEMBER_CHECKER(id)
int main()
{
static_assert(
HAS_MEMBER(X, id) && is_integral<MEMBER_TYPE(X, id)>::value,
"Error!"
);
}
这篇关于如何检测给定名称的成员变量的存在和类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!