c++ - 试图强制静态对象初始化

标签 c++ templates initialization static-members

我试图初始化一个静态对象但没有成功。目的是在存储库中自动注册一个工厂类(这是一个单例)。

我已经看过了:How to force a static member to be initialized?

其中一条评论说(我也遵循了一个示例):

I read it up in the C++ standard (14.7.1): Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

所以我正在尝试做类似的事情,但我还没有设法强制对象初始化。这是代码。我不知道我错过了什么。这是我正在使用的模板。

namespace my_lib
{
    template <typename T>
    struct FactoryHelper
    {
        FactoryHelper ();
        static FactoryHelper<T> _helper;
    };
}

这是库的用户用来定义工厂类并同时在存储库中注册对象的宏:

#define CREATE_FACTORY(ClassName)\
namespace my_lib\
{\
    class ClassName##Factory;\
    template<> FactoryHelper<ClassName##Factory>::FactoryHelper () { std::cout << "object initialized!" << std::endl; }\
    template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper;\
    struct ClassName##Factory : public FactoryBase<ClassName> {\
      ...\
    };\
} 

前面的代码是在头文件(Factory.h)中定义的。

在 .cpp 文件 (Example.cpp) 中,我有:

CREATE_FACTORY(UnitTestExample)
...

当我执行程序时,我看不到调用构造函数时打印的消息。我们非常欢迎任何帮助。

提前致谢。

最佳答案

这是 C++ 的一个棘手领域。您所做的是尝试在此处定义静态成员:

template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper;\

但这实际上是声明而不是定义。对于 C++ 将其视为定义,您必须将某些内容传递给构造函数。通常,这是您要将其初始化为的值:

template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper = FactoryHelper<ClassName##Factory>();\

但在您的情况下,您希望它是一个单例,因此您可能不希望它是可复制的。在这种情况下,您需要一些虚拟参数:

template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper(0);\

并且你必须适本地修改你的构造函数:

template<> FactoryHelper<ClassName##Factory>::FactoryHelper (int) { std::cout << "object initialized!" << std::endl; }\

这是完整的工作示例:

#include <iostream>

namespace my_lib
{
    template<typename> struct FactoryBase { };
    template <typename T>
    struct FactoryHelper
    {
        FactoryHelper (int);
        static FactoryHelper<T> _helper;
    };
}

#define CREATE_FACTORY(ClassName)\
namespace my_lib\
{\
    class ClassName##Factory;\
    template<> FactoryHelper<ClassName##Factory>::FactoryHelper (int) { std::cout << "object initialized!" << std::endl; }\
    template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper(0);\
    struct ClassName##Factory : public FactoryBase<ClassName> {\
    };\
} 

struct UnitTestExample {
};

CREATE_FACTORY(UnitTestExample);

int main(int argc,char **argv)
{
  return 0;
}

也就是说,使用其他答案中的一些建议可能是更好的设计决策。

可以在此处找到有关显式特化声明与定义的更多信息:static member initialization for specialized template class

关于c++ - 试图强制静态对象初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9549714/

相关文章:

c - C 是否将结构填充初始化为零?

C++ vector 初始化

c++ - 对使用 unique_ptr 和自定义删除器感到困惑

c++ - 为双 vector 创建自己的迭代器

c++ - 如何制作一个函数模板,它接受一个带有可变参数的仿函数

c++ - 如何从双参数模板创建单参数模板以用作基类模板模板参数

c++ - 如何在 g++ 中抑制 ' left shift count >= width of type' 警告

c++ - 重载取消引用运算符

函数调用运算符的 C++ 模板

c++ - 使用常量长度时 char[] 和 new char[] 之间的区别