C ++未命名(匿名)名称空间定义 [英] C++ unnamed(anonymous) namespace definition

查看:92
本文介绍了C ++未命名(匿名)名称空间定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 03 Standard 7.3.1.1 [namespace.unnamed]第1段:(和C ++ 11 Standard也使用类似的定义)

C++03 Standard 7.3.1.1 [namespace.unnamed] paragraph 1: (and C++11 Standard also use similar definition)

未命名空间定义的行为就好像被替换为

An unnamed-namespace-definition behaves as if it were replaced by

namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }

为什么不仅仅遵循定义?

Why not is it simply following definition?

namespace unique { namespace-body }
using namespace unique;

侧面问题: MSDN 由后一种形式定义.它在技术上是否违反标准?

Side question: MSDN defines by latter form. Does it violate Standard technically?

推荐答案

您不能再这样做

namespace { typedef int a; ::a x; }

请注意,在随后的namespace { ... }中,您突然可以.这将是非常不一致的.

Note that in a subsequent namespace { ... }, suddenly you could. This would be horribly inconsistent.

还要注意这种情况,具有两个不同的有效结果

Also notice this case, with two different valid outcomes

namespace A { void f(long); }
using namespace A;

namespace { 
  void f(int);
  void g() {
    ::f(0);
  }
}

对于ISO C ++,这将调用fint版本.使用您的替代定义,它称为long版本.

With ISO C++, this calls the int version of f. With your alternative definition, it calls the long version.

这篇关于C ++未命名(匿名)名称空间定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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