强制静态类成员初始化,即使未明确包含在任何文件中

时间:2019-01-22 12:47:38

标签: c++ templates static include data-driven

我有一个基类:

template <typename T>
class Base
{
    public:
        struct RegisterType {
            RegisterType() { RegisterHelper::get().registerType<T>();}
        };

        static RegisterType s_register;
};

template <typename T>
typename Base<T>::RegisterType Base<T>::s_register __attribute__((used));

和派生类:

class Derived : public Base<Derived>
{
    public:
        constexpr static const char *const propertyA = "propertyA";
        ...
}

需要注册派生类型,以便将某些属性映射到派生类型id,从而使数据驱动器更简洁。但是,我不想在外部类中显式注册它们,因此我的目标是派生类型注册在派生类型定义内部。

我发现的问题是,在项目代码中没有使用某些派生类,因此,这些派生类未注册,这导致装入程序中的最终结果不完整。如果我明确包含有问题的Derived类型(即DerivedOne),则此问题已解决,例如:'#include“ DerivedOne.h”'

是否有任何方法可以强制其初始化而不必显式包括所有派生类?

预先感谢

0 个答案:

没有答案