Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;在另一个名称空间(不是封闭的)中通过“定义类”;使用;_C++_Class_Namespaces_Definition - Fatal编程技术网

C++ c++;在另一个名称空间(不是封闭的)中通过“定义类”;使用;

C++ c++;在另一个名称空间(不是封闭的)中通过“定义类”;使用;,c++,class,namespaces,definition,C++,Class,Namespaces,Definition,g++、icc和clang接受以下代码: namespace ns { struct A ; } namespace ns1 // note : not enclosing namespace { using namespace ns ; struct A { } ; } 根据标准,这是有效代码吗 [名称空间。qual]7: ... 但是,在此类命名空间成员声明中 嵌套名称说明符可能依赖于使用指令隐式 提供嵌套名称说明符的起始部分 如相关文件所述 可以省略嵌套名称说明符

g++、icc和clang接受以下代码:

namespace ns { struct A ; }
namespace ns1 // note : not enclosing namespace
{ 
    using namespace ns ;
    struct A { } ;
}  
根据标准,这是有效代码吗


[名称空间。qual]7: ... 但是,在此类命名空间成员声明中 嵌套名称说明符可能依赖于使用指令隐式 提供嵌套名称说明符的起始部分

如相关文件所述

可以省略嵌套名称说明符的起始部分,但不能省略任何中间部分


(我从哪里得到的信息是,它可以/必须在封闭的名称空间中完成,我不记得了。)

代码符合要求,但可能没有达到您的预期

namespace ns 
{ 
    struct A ;      // declaration of ns::A
}
namespace ns1
{ 
    using namespace ns ;
    struct A { } ;  // definition of ns1::A, which has nothing to do with ns::A
} 

请注意,名称空间中的任何声明都将成为该名称空间的成员,因此
ns::A
ns1::A
是两个不相关的
struct
s。

代码编译得很好,但我同意@songyuangyao的回答,同时也要注意隐藏

比如说,

namespace ns 
{ 
   struct A ;
  /* Calling A inside this scope will definitely refer to struct A defined in
     this(ns) namespace, and everytime you want to access struct A of ns1, 
     you need to call it via ns1::A otherwise it will refer to ns::A (which is defined in 
     this namespace.
  */
}    
namespace ns1
{ 
   using namespace ns ;
   struct A { } ;
}