使用模板初始化Globals [英] Globals initialization using templates

查看:85
本文介绍了使用模板初始化Globals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个主题有点误导,因为我真的无法弄清楚以下代码片段正在做什么

并且非常感谢

解密它。


我的意思是我可以理解代码正在发生什么,但我错过了

更大的图片。 />

在什么情况下我会用这个?


模板< typename T>

class Globals

{

public:

Globals()

{

if(impl == NULL)

{

init();

}

}

T * operator- >()const

{

返回impl;

}

私人:

static T * impl;

static void init()

{

静态联盟

{

char buf [sizeof(T)];

double dummyForAlignment;

}数据;

impl = new(数据.buf)T;

}

};


temp晚期< typename T>

T * Globals< T> :: impl = NULL;

解决方案



Dilip写道:


这个主题有点误导,因为我真的不知道是什么

以下代码片段正在做什么,并希望在

解密它的任何帮助。


我的意思是我可以理解代码方式发生了什么但是我我错过了

大图。


在什么情况下我会用它?


模板< typename T> ;

类Globals

{

public:

Globals()

{

if(impl == NULL)

{

init();

}

}

T * operator->()const

{

return impl;

}

私人:

静态T * impl;

static void init()

{

stati c union

{

char buf [sizeof(T)];

double dummyForAlignment;

}数据;

impl = new(data.buf)T;

}

};


template< ; typename T>

T * Globals< T> :: impl = NULL;



在多线程环境中定义Globals(即使脚趾代码

这里没有任何多线程结构)你可以控制

初始化的顺序,特别是当你看起来很可能

有一堆全局变量相互依赖时。


< blockquote> ampar ... @ gmail.com写道:


在多线程环境中定义Globals(即使脚趾代码

这里没有任何多线程构造)你可以控制初始化的顺序,特别是当你可能会因为一堆全局变量而彼此依赖时,b $ b。



你能给我一个简短的例子吗?


我认为定义了全局变量的初始化顺序

翻译单元内?

你的意思是翻译单位定义全局变量的情况

最终会因为初始化而相互依赖吗?如果是这样,该类如何解决问题?


Dilip写道:


这个主题有点误导,因为我真的无法弄清楚以下代码片段正在做什么

并且非常感谢
$ b $中的任何帮助b解密它。


我的意思是我可以理解代码正在发生的事情,但我错过了

更大的图片。


在什么情况下我会使用它?


模板< typename T>

类Globals

{

public:

Globals()

{

if(impl == NULL)

{

init();

}

}

T * operator->() const

{

return impl;

}



//缺少析构函数

//如果T管理某种资源,这是必要的。

~Globals()

{

impl-> T :: ~T();

}


private:

static T * impl;

static void init()

{

静态联盟

{

char buf [sizeof(T)];

double dummyForAlignment;

}数据;

impl = new(data.buf)T;

}

};


模板< typename T>

T * Globals< T> :: impl = NULL;



The subject is a bit misleading because I really can''t figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typename T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}
private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignment ;
} data ;
impl = new (data.buf) T;
}
};

template<typename T>
T* Globals<T>::impl = NULL;

解决方案


Dilip wrote:

The subject is a bit misleading because I really can''t figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typename T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}
private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignment ;
} data ;
impl = new (data.buf) T;
}
};

template<typename T>
T* Globals<T>::impl = NULL;

Defining Globals in a multithreaded environment (even though toe code
here doesnt have any multi threading constructs)where you could control
the order of Initialization especially when you oculd be possibly
having a bunch of globals depending upon each other.


ampar...@gmail.com wrote:

Defining Globals in a multithreaded environment (even though toe code
here doesnt have any multi threading constructs)where you could control
the order of Initialization especially when you oculd be possibly
having a bunch of globals depending upon each other.

can you give me a short example?

I thought the order of initialization for global variables are defined
within a translation unit?
Do you mean cases where global vars defined across translation units
somehow end up depending on one another for initialization? If so, how
does that class solve the problem?


Dilip wrote:

The subject is a bit misleading because I really can''t figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.

I mean I can understand code-wise what is going on but I am missing the
larger picture.

In what circumstances would I use this?

template<typename T>
class Globals
{
public:
Globals()
{
if (impl == NULL)
{
init();
}
}
T* operator->() const
{
return impl;
}

// missing destructor
// this is necessary in case T manages some kind of resource.
~Globals()
{
impl->T::~T();
}

private:
static T* impl;
static void init()
{
static union
{
char buf[ sizeof( T ) ] ;
double dummyForAlignment ;
} data ;
impl = new (data.buf) T;
}
};

template<typename T>
T* Globals<T>::impl = NULL;


这篇关于使用模板初始化Globals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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