全局常量字符串& 我闻起来很臭,真的安全吗?
他在全局范围内定义了几个常量:
const string& SomeConstant = "This is some constant text";
我正在审查一位同事的代码,我发现 字符数组。
从语法上讲,它是合法的(至少在 VC++ 7 中),并且似乎可以运行,但实际上我宁愿让他删除 & 。 所以它在做什么是没有歧义的。
那么,这真的安全合法而且我很着迷吗? 正在构造的临时对象是否有保证的生命周期? 我一直假设以这种方式使用的匿名对象在使用后会被破坏......
所以我的问题也可以推广到匿名对象的生命周期。 标准是否规定了匿名对象的生命周期? 它与同一范围内的任何其他对象具有相同的生命周期吗? 或者只给出表达式的生命周期?
另外,当作为本地人执行此操作时,其范围显然有所不同:
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(外部); 破坏A(外层); 你好
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.
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;
}
Shows:
Constructing A(Outer);
Destructing A(Outer);
Hi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是完全合法的。 直到程序结束它才会被破坏。
编辑:是,保证:
-- 2008 年工作草案,编程语言 C++ 标准,第 3.7.1 页,第 63 页
正如 Martin 指出的那样,这并不是完整的答案。标准草案进一步指出(第 12.2 页,第 250-1 页):
我在 g++ 中进行了测试,这是否会让您感觉好一些。;)
It's completely legal. It will not be destructed until the program ends.
EDIT: Yes, it's guaranteed:
-- 2008 Working Draft, Standard for Programming Language C++, § 3.7.1 p. 63
As Martin noted, this is not the whole answer. The standard draft further notes (§ 12.2, p. 250-1):
I tested in g++ if that makes you feel any better. ;)
是的,它是有效且合法的。
因此,您正在创建一个临时对象。
这个临时对象绑定到一个 const& 因此它的生命周期也延长到了它所绑定的变量的生命周期(即比创建它的表达式长)。
这是由标准保证的。
注意:
虽然这是合法的。 我不会使用它。 最简单的解决方案是将其转换为 const std::string。
用法:
在这种情况下,因为变量在全局范围内,所以它在程序的整个长度内有效。 因此,一旦执行进入 main() 就可以使用它,并且在执行退出 main() 后不应该访问它。
尽管它在技术上可能在此之前可用,但您在全局对象的构造函数/析构函数中使用它应该受到全局变量初始化顺序的已知问题的影响。
额外的想法:
另一方面,这不会遇到问题:
并且可以在任何时候使用。 只是一个想法。
Yes it is valid and legal.
Thus you are creating a temporary object.
This temporary object is bound to a const& and thus has its lifetime extended to the lifespan of the variable it is bound too (ie longer than the expression in which it was created).
This is guranteed by the standard.
Note:
Though it is legal. I would not use it. The easist solution would be to convert it into a const std::string.
Usage:
In this situation because the variable is in global scope it is valid for the full length of the program. So it can be used as soon as execution enters main() and should not be accessed after executiuon exits main().
Though it technically may be avilable before this your usage of it in constructors/destructors of global objects should be tempered with the known problem of global variable initialization order.
Extra Thoughts:
This on the other hand will not suffer from the problem:
And can be used at any point. Just a thought.
这可能是合法的,但仍然很丑陋。 省略参考!
It might be legal, but still ugly. Leave out the reference !
它既合法又丑陋。
It's as legal as it's ugly.
使用
const
引用扩展临时变量是合法的,这是 Alexandrescu 的 使用的ScopeGaurd 看到了 Herb Sutter 的精彩解释,称为 “最重要的const 的候选者”
”。话虽这么说,这个特定案例滥用了 C++ 的这一功能,应该删除引用,留下一个简单的
const string
。It's legal to extend a temporary variable with a
const
reference, this is used by Alexandrescu's ScopeGaurd see this excellent explanation by Herb Sutter called A candidate for the "Most importantconst
".That being said this specific case is an abuse of this feature of C++ and the reference should be removed leaving a plain
const string
.通过将其声明为 const(这意味着它无法更改),然后将其设为引用(这意味着有人可能会更改它),至少看起来是一种不好的形式。 另外,我相信您也明白,全局变量是不好的,而且很少有必要。
By declaring it as const (which means it can't be changed) and then making it a reference, which implies that someone might change it, seems like bad form, at the very least. Plus, as I am sure you understand, global variables are BAD, and rarely necessary.
好吧,如果我说得离题了,请大家纠正我,但这是我听了你们所有精彩回答后得出的结论:
A) 它在语法和逻辑上都是合法的,& 是合法的。 将临时/匿名的生命周期从超出表达式级别延长到引用的生命周期。 我在 VC++7 中验证了这一点:
B) 虽然它是合法的,但它可能会导致对实际生命周期的一些混乱,并且在这些情况下的引用不会给你声明它为非引用的任何好处,因此引用也许应该避免,甚至可能是额外的空间。 既然没有任何好处,那就是不必要的混淆。
感谢所有的答案,这是一次非常有趣的讨论。 所以总而言之:是的,它在语法上是合法的,不,随着生命周期的延长,它在技术上并不危险,但它没有增加任何东西,并且可能会增加成本和混乱,所以为什么要麻烦呢。
听起来对吗?
Okay, folks correct me if I'm off the deep end, but here's my conclusions listening to all of your excellent responses:
A) it is syntactically and logically legal, the & extends the lifetime of the temp/anonymous from beyond expression level to the life of the reference. I verified this in VC++7 with:
B) Though it is legal, it can lead to some confusion as to the actual lifetime and the reference in these cases give you no benefit of declaring it as a non-reference, thus the reference should probably be avoided and may even be extra space. Since there's no benefit to it, it's unnecessary obfuscation.
Thanks for all the answers it was a very interesting dicussion. So the long and short of it: Yes, it's syntactically legal, no it's not technically dangerous as the lifetime is extended, but it adds nothing and may add cost and confusion, so why bother.
Sound right?