全局const字符串&闻到对我不好,是真的安全吗? [英] Global const string& smells bad to me, is it truly safe?

查看:123
本文介绍了全局const字符串&闻到对我不好,是真的安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看同事的代码,我看到他在全局范围中定义了几个常量:

I'm reviewing a collegue's code, and I see he has several constants defined in the global scope as:

const string& SomeConstant = "This is some constant text";

个人来说,这对我来说很臭,因为参考是指我假设的是

Personally, this smells bad to me because the reference is referring to what I'm assuming is an "anonymous" object constructed from the given char array.

在语法上,它是合法的(至少在VC ++ 7中),它似乎运行,但真的我宁愿他删除&所以,它在做什么没有歧义。

Syntactically, it's legal (at least in VC++ 7), and it seems to run, but really I'd rather have him remove the & so there's no ambiguity as to what it's doing.

所以,这是真正的安全和合法的,我痴迷?所构造的临时对象是否具有保证的生命周期?我一直假设匿名对象以这种方式使用后被破坏使用后...

So, is this TRULY safe and legal and I'm obsessing? Does the temp object being constructed have a guaranteed lifetime? I had always assumed anonymous objects used in this manner were destructed after use...


所以我的问题也可以推广到匿名对象生命周期。标准是否规定匿名对象的生命周期?它将与同一作用域中的任何其他对象具有相同的生存期吗?或者是只给定表达式的生命周期吗?

So my question could also be generalized to anonymous object lifetime. Does the standard dictate the lifetime of an anonymous object? Would it have the same lifetime as any other object in that same scope? Or is it only given the lifetime of the expression?


作为一个本地,它的显然范围是不同的:

Also, when doing it as a local, it's obviously scoped differently:

class A
{
    string _str;

public:
    A(const string& str) :
        _str(str)
    {
    	cout << "Constructing A(" << _str << ")" << endl;
    }

    ~A()
    {
    	cout << "Destructing A(" << _str << ")" << endl;
    }
};

void TestFun()
{
    A("Outer");
    cout << "Hi" << endl;
}

显示:

构造A(外部);
Destructing A(Outer);
Hi

Constructing A(Outer); Destructing A(Outer); Hi

推荐答案

这是完全合法的。

编辑:,它可以保证:


没有动态
存储时间的所有对象,没有线程
存储持续时间,并且不是本地
具有静态存储持续时间。这些对象的
存储将在程序
(3.6.2,3.6.3)的持续时间内持续

"All objects which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3)."

- 2008年工作草案,标准用于编程语言C ++ ,第3.7.1节。 63

-- 2008 Working Draft, Standard for Programming Language C++, § 3.7.1 p. 63

正如马丁所说,这不是整个答案。标准草案进一步说明(第12.2节,第250-1页):

As Martin noted, this is not the whole answer. The standard draft further notes (§ 12.2, p. 250-1):


类类型的临时表被创建
各种上下文:将值
绑定到引用(8.5.3)[...]即使
避免创建临时对象
(12.8),所有语义
限制应被尊重,如同
临时对象已创建
[...]临时对象被销毁
作为评估
全表达式的最后一步(...)有两个上下文
,其中临时被销毁在
a的当前点,而不是($)
创建的
。 [...]第二个
上下文是当一个引用将
绑定到一个临时的时候,引用绑定的临时变量

临时变量是
绑定的子对象的完整对象

的生命周期内持续存在,除非下面指定

"Temporaries of class type are created in various contexts: binding an rvalue to a reference (8.5.3) [...] Even when the creation of the temporary object is avoided (12.8), all the semantic restrictions shall be respected as if the temporary object had been created. [...] Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. [...] There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. [...] The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except as specified below."

我在g ++测试,如果这让你感觉更好。 ;)

I tested in g++ if that makes you feel any better. ;)

这篇关于全局const字符串&amp;闻到对我不好,是真的安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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