如何检测一个类是否有成员变量? [英] How to detect if a class has member variables?

查看:46
本文介绍了如何检测一个类是否有成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

这篇关于如何检测一个类是否有成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆