如何检测一个类是否有成员变量?

编程入门 行业动态 更新时间:2024-10-23 09:22:43
本文介绍了如何检测一个类是否有成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想检测一个类是否有成员变量,如果有的话,静态断言失败.类似的东西:

I would like to detect if a class has member variables and fail a static assert if they do. Something like:

struct b { int a; } static_assert(!has_member_variables<b>, "Class should not contain members"). // Error. struct c { virtual void a() {} void other() {} } static_assert(!has_member_variables<c>, "Class should not contain members"). // Fine. struct d : c { } static_assert(!has_member_variables<d>, "Class should not contain members"). // Fine. struct e : b { } static_assert(!has_member_variables<e>, "Class should not contain members"). // Error. struct f : c { char z; } static_assert(!has_member_variables<f>, "Class should not contain members"). // Error.

有没有办法用 SFINAE 模板来实现这一点?这个类可能有继承甚至多重继承和虚函数(尽管基类中没有成员).

Is there a way to achieve this with SFINAE template? This class may have inheritance or even multiple inheritance with virtual functions (no members in the base classes though).

我有一个非常简单的设置,如下所示:

I have a pretty simple setup as follows:

class iFuncRtn { virtual Status runFunc(Data &data) = 0; }; template <TRoutine, TSpecialDataType> class FuncRoutineDataHelper : public iFuncRtn { Status runFunc(Data &data) { static_assert(!has_member_variables<TRoutine>, "Routines shouldnt have data members!"); // Prepare special data for routine TSpecialDataType sData(data); runFuncImpl(sData); } class SpecificRtn : public FuncRoutineDataHelper<SpecificRtn, MySpecialData> { virtual Status runFuncImpl(MySpecialData &sData) { // Calculate based on input sData.setValue(someCalculation); } };

FunctionalityRoutine 以每个滴答为基础进行管理和运行.它们是定制的,可以执行各种各样的任务,例如联系其他设备等.传入的数据可以由例程操作,并保证在每次滴答执行时传入,直到功能完成.基于 DataHelper 类传入正确类型的数据.我不想阻止未来的人们错误地将数据添加到功能例程中,因为这不太可能达到他们的预期.为了强制执行此操作,我希望找到一种使用静态断言的方法.

The FunctionalityRoutines are managed and run on a per tick basis. They are customized and can perform a wide variety of tasks such as contacting other devices etc. The data that is passed in can be manipulated by the routine and is guaranteed to be passed in on each tick execution until the functionality is finished. The right type of data is passed in based on the DataHelper class. I wan't to discourage future people from mistakenly adding data to the functionality routines as it is very unlikely to do what they expect. To force this, I was hoping to find a way with static assert.

推荐答案

您可以通过依赖编译器进行空基类优化来解决此问题,方法是检查从您的 T 派生的类是否具有与具有虚函数的空类大小相同:

You can solve this by depending on the compiler doing empty base class optimizations, by checking if a class derived from your T has the same size as an empty class with virtual functions:

template<typename T, typename... BaseClasses> class IsEmpty { // sanity check; see the updated demo below static_assert(IsDerivedFrom<T, BaseClasses...>::value); struct NonDerived : BaseClasses... { virtual ~NonDerived() = default; }; struct Derived : T { virtual ~Derived() = default; }; public: inline static constexpr bool value = (sizeof(NonDerived) == sizeof(Derived)); };

这应该适用于单继承和多继承.但是,在使用多重继承时,需要列出所有基类,如下所示:

This should work with both single and multiple inheritance. However, when using multiple inheritance, it's necessary to list all base classes, like that:

static_assert(IsEmpty<Derived, Base1, Base2, Base3>::value);

显然,这个解决方案排除了 final 类.

Obviously, this solution rules out final classes.

这是更新后的演示.

这是原始演示.(不适用于多重继承)

Here's the original demo. (doesn't work with multiple inheritance)

更多推荐

如何检测一个类是否有成员变量?

本文发布于:2023-11-11 07:50:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577799.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!