如何检测类中是否存在特定的PRIVATE成员变量? [英] How to detect whether there is a specific PRIVATE member variable in class?
问题描述
目标:我想知道一个类是否具有成员变量 x
。我希望收到 true
,无论此变量是否是 private
, public
或受保护的
。
Goal: I would like to know if a class has the member variable x
. I would like to receive true
regardless whether or not this variable is private
, public
or protected
.
方法:如果某个班级有使用以下代码的成员变量:
Approach: You can get the information if a class has a member variable using the following code:
template <typename T, typename = int>
struct HasX : std::false_type { };
template <typename T>
struct HasX <T, decltype((void) T::x, 0)> : std::true_type { };
与constpr一起使用
Use it with
if constexpr (HasX<my_class>::value) {
// do stuff with x
} else {
// ...
}
上面的代码在这种情况下不起作用
The above code does not work in this case
struct my_class {
private:
int x;
};
如何进行这项工作?我希望 HasX< my_class> :: value
为 true
。
How can I make this work? I would like HasX<my_class>::value
to be true
.
想法:
使用有权访问 T的
。这似乎不起作用。查看此在线示例。 friend
类: :x
Use a friend
class which has access to T::x
. This does not seem to work. Check out this live example.
推荐答案
好吧...不确定此解决方案的正确性和局限性...但是...
Well... not sure about correctness and limits of this solution... but...
如果用<$定义了一个辅助结构c $ c> x 元素可访问
struct check_x_helper
{ int x; };
您可以编写从 check_x_helper
和您要查看的类是否包含 x
成员
you can write a template struct that inherit from both check_x_helper
and the class you want to see if contain a x
member
template <typename T>
struct check_x : public T, check_x_helper
内部 check_x
您可以如下声明(仅声明:在 decltype()
中使用)
Inside check_x
you can declare (declare only: are used inside a decltype()
) as follows
template <typename U = check_x, typename = decltype(U::x)>
static constexpr std::false_type check (int);
static constexpr std::true_type check (long);
观察第一个,即模板:当选中的类( T
)包含 x
成员, decltype(U :: x)
含糊不清,因为 x
继承自 T
和 check_x_helper
,因此该函数被SFINAE丢弃
Observe the first one, the template one: when the checked class (T
) contains an x
member, the decltype(U::x)
is ambiguous because x
is inherited from both T
and check_x_helper
, so this function is SFINAE discarded.
相反,当 T
不包含 x
成员时,没有任何歧义, decltype(U :: x)
是 check_x_helper :: x
的类型( int
)和第一个 check()
功能保持启用状态。
On contrary, when T
doesn't contains an x
member, there isn't an ambiguity, the decltype(U::x)
is the type of check_x_helper::x
(int
) and the first check()
function remain enabled.
现在,您需要使用type = decltype(check(0));
Now you need something as
using type = decltype(check(0));
static constexpr auto value = type::value;
调用 check(0)
( int
参数表示对模板版本的偏好),并将检测到的值
保存在静态constexpr $中
to call check(0)
(the int
parameter express the preference to the template version) and save the detected value
in a static constexpr
variable.
以下是完整的编译示例
#include <iostream>
#include <utility>
class foo
{ int x; };
struct bar
{ };
struct check_x_helper
{ int x; };
template <typename T>
struct check_x : public T, check_x_helper
{
template <typename U = check_x, typename = decltype(U::x)>
static constexpr std::false_type check (int);
static constexpr std::true_type check (long);
using type = decltype(check(0));
static constexpr auto value = type::value;
};
int main()
{
std::cout << check_x<foo>::value << std::endl;
std::cout << check_x<bar>::value << std::endl;
}
此解决方案的缺点: decltype(U :: x)当
类型。因此,给定 T
声明 x
作为方法或<$ c $时,code>也失败(含糊) c>使用
Drawback of this solution: decltype(U::x)
fail (ambiguity) also when T
declare x
as a method or as a using
type. So given
class foo
{ int x () { return 0;} ; };
或
class foo
{ using x = int; };
从 check_x< foo> :: value
中获得 1
。
这篇关于如何检测类中是否存在特定的PRIVATE成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!