匿名名称空间中的对象是否隐式静态? [英] Are objects in anonymous namespace implicitly static?

查看:52
本文介绍了匿名名称空间中的对象是否隐式静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C ++ 11标准的角度来看,对象状态/属性之间存在技术上的区别:

From the C++11 standard point of view, is there a technical difference of object status/properties between:

namespace
{
   int foo;
   const int bar = 42;
}

namespace
{
   static int foo;
   static const bar = 42;
}

从这里的问题和答案中,我相信匿名名称空间中的对象是隐式静态的,但是有人告诉我,这只是内部的链接,编译器不会像对待它一样使用该对象。被标记为静态,意味着它将如何实现对象构造。因此,如果匿名名称空间中的静态和非静态之间存在区别,我需要一些真正含义的详细信息。

From questions and answers around here I was believing that objects in anonymous namespace were implicitly static, but someone tell me it's the linkage only that's internal, the compiler will not take the object as if it was marked static with implications like how it will implement object construction. So I need some details about what it really means, if there is a difference between with and without static in anonymous namespace.

推荐答案

C ++ 11,3.5 / 4:

C++11, 3.5/4:


未命名空间或在未命名空间中直接或间接声明的
具有内部联系。所有其他命名空间
具有外部链接。如果上面的内部链接给出的名称空间范围不是
,那么它的名称与封闭的
名称空间具有相同的链接,如果它是—变量的名称...

An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage. A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of — a variable ...

因此,在C ++ 11中,您的两个 foo 对象都具有内部链接。在C ++ 03中,第一个具有外部链接。

So in C++11 both of your foo objects have internal linkage. In C++03, the first one has external linkage.

与链接无关,它具有静态存储持续时间。

Regardless of linkage, it has static storage duration.

我认为标准中没有将对象当作标记为静态对象这样的东西,所以我无法回答。如果您在标准中找到一些有关对象是否被标记为静态的文字,并且没有专门提及链接或存储持续时间,请引用该文字,我可以给您一个意见:-)

I don't think there's any such thing in the standard as "take the object as if it was marked static", so I can't answer to that. If you find some text in the standard that refers to whether the object is "marked static" and doesn't refer specifically to either linkage or storage duration, then cite it and I can give you an opinion :-)

这篇关于匿名名称空间中的对象是否隐式静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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