试图强制静态对象初始化 [英] trying to force static object initialization

查看:191
本文介绍了试图强制静态对象初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to initialize a static object without success. The purpose is to automatically register a factory class in a repository (which is a singleton).

我已经看过了:如何强制静态成员初始化?

其中一个意见说,(还有一个例子,我遵循):

One of the comments says that (there is also an example that I've followed):


在C ++标准(14.7.1)中读取它:除非类模板或成员模板的成员已被显式实例化或显式专用化,否则当在需要特殊化的上下文中引用特化时,成员定义存在;特别是,静态数据成员的初始化(和任何相关的副作用)不会发生,除非静态数据成员本身以需要静态数据成员的定义的方式使用。

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.

所以我想做类似的事情,但我没有设法强制对象初始化。这里是代码。我不知道我失踪了。这是我使用的模板。

So I'm trying to do something similar but I haven't manage to force the object initialization. Here is the code. I don't know what I'm missing. This is the template I'm using.

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

这是库的用户将用来定义工厂类,同时在库中注册一个对象:

And this is the macro that the user of the library would use to define the factory class and, at the same time, register an object in the repository:

#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> {\
      ...\
    };\
} 


$ b b

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

The previous code is defined in a header file (Factory.h).

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

In a .cpp file (Example.cpp), I have:

CREATE_FACTORY(UnitTestExample)
...

当我执行程序时,无法看到构造函数在调用时打印的消息。

When I execute the program, I cannot see the message that the constructor prints when it is invoked. Any help is more than welcome.

提前感谢。

推荐答案

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

This is a tricky area of C++. What you've done is to try to define the static member here:

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

但这实际上是一个声明一个定义。对于C ++把它当作一个定义,你必须传递一些东西到构造函数。通常,这是您要初始化为:

but this is actually a declaration and not a definition. For C++ to treat it as a definition you have to pass something to the constructor. Typically, this is the value you want to initialize it to:

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

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

But in your case, you want this to be a singleton, so you probably don't want it to be copyable. In that case, you need some dummy parameter:

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;
}

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

That said, using some of the suggestions in the other answers may be a better design decision.

有关显式特殊化声明与定义的更多信息可以在这里找到:

More information on the explicit specialization declaration vs. definition can be found here: static member initialization for specialized template class

这篇关于试图强制静态对象初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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