为什么内联未命名命名空间? [英] Wherefore inline unnamed namespaces?

查看:195
本文介绍了为什么内联未命名命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个快速的专家:C ++ 11允许未命名的命名空间声明为 inline 。这对我来说似乎是多余的;



所以我的问题是这是什么意思:

  inline namespace / * anonymous * / {
// stuff
}

与传统

有什么不同?

  namespace / * anonymous * / {
// stuff
}

我们知道和爱从C ++ 98?在使用 inline 时,任何人都可以举例说明不同的行为吗?



EDIT:只是为了澄清,因为这个问题已被标记为重复:我不是问一般命名内联命名空间。我理解这里的用例,我认为他们是伟大的。我特别要求声明一个未命名的命名空间为 inline 的含义。因为未命名的命名空间必须总是在一个TU的本地,符号版本理性似乎不适用,所以我很好奇什么添加 inline 实际上 / em>。






另外,关于未命名命名空间的标准[7.3.1.1] p>


inline 出现,如果它出现在 unnamed-namespace-


但这似乎对我的非语言律师的眼睛是一个重言式 - 它出现在定义iff它出现在定义!

编辑: Cubbi声称在评论中的奖励积分:

/ p>


标准是说未命名命名空间定义的行为好像被 X 其中 inline 出现在 X 中,如果它出现在 unnamed-namespace-definition



解决方案

这里是我发现的一个用法:

  namespace widgets {inline namespace {

void foo();

}} //命名空间

void widgets :: foo()
{
}

在这个例子中, foo 有内部链接,我们可以稍后使用 namespace :: function 语法,以确保函数的签名正确。如果你不使用 widgets 命名空间,那么 void foo()定义将定义一个完全不同的函数。



如果还有另一个名为 foo 命名空间已经然后这将给你一个歧义,而不是一个讨厌的ODR违反。


A quick one for the gurus: C++11 allows unnamed namespaces to be declared inline. This seems redundant to me; things declared in an unnamed namespace are already used as if they were declared in the enclosing namespace.

So my question is this: what does it mean to say

inline namespace /*anonymous*/ {
    // stuff
}

and how is it different from the traditional

namespace /*anonymous*/ {
    // stuff
}

that we know and love from C++98? Can anyone give an example of different behaviour when inline is used?

EDIT: Just to clarify, since this question has been marked as a duplicate: I'm not asking about named inline namespaces in general. I understand the use-case there, and I think they're great. I'm specifically asking what it means to declare an unnamed namespace as inline. Since unnamed namespaces are necessarily always local to a TU, the symbol versioning rational doesn't seem to apply, so I'm curious about what adding inline actually does.


As an aside, the standard [7.3.1.1], regarding unnamed namespaces, says:

inline appears if and only if it appears in the unnamed-namespace-definition

but this seems like a tautology to my non-language lawyer eyes -- "it appears in the definition iff it appears in the definition"! For bonus points, can anyone explain what this bit of standardese is actually saying?

EDIT: Cubbi claimed the bonus point in the comments:

the standard is saying that unnamed-namespace-definition behaves as if it were replaced by X where inline appears in X iff it appears in the unnamed-namespace-definition

解决方案

Here is one use that I have found:

namespace widgets { inline namespace {

void foo();

} } // namespaces

void widgets::foo()
{
}

In this example, foo has internal linkage and we can define the function later on by using the namespace::function syntax to ensure that the function's signature is correct. If you were to not use the widgets namespace then the void foo() definition would define a totally different function. You also don't need to re-open the namespace saving you a level of indentation.

If there is another function called foo in the widgets namespace already then this will give you an ambiguity instead rather than a nasty ODR violation.

这篇关于为什么内联未命名命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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