C++ 命名空间名称和 class 名称重复是否会导致冲突?
Does C++ namespace name and class name duplication lead to conflict?
我正在测试 C++ 的名称查找规则。我有一个包含 3 个文件的简单程序:
$cat testns01.h
struct S{
static int f(){return 1;}
};
$cat testns02.h
namespace S{
static int f(){return 2;}
}
$cat testns3.cpp
#include "testns02.h" // namespace
#include "testns01.h" // struct
#include<stdio.h>
int main()
{
int i = S::f();
printf("%d\n",i);
return 0;
}
如果我编译 运行,我得到:
$g++ testns3.cpp && ./a.out
1
好的,我有 3 个问题:
- name "S" class name 和 namespace 之间的重复不冲突?
- 当两者都具有名称 "S" 时,似乎 "struct S" 具有更高的优先级
如果我注释掉#include "testns01.h"这一行,程序会打印2,还是可以的。所以我的第三个问题是:
- c++ 标准是否讨论名称查找如何解决名称重复问题?
- name "S" duplication between class name and namespace doesn't conflict?
他们有。
- When both has name "S", seems "struct S" has higher priority
没有。 (往下看)
- Does c++ stardard talke about how name lookup resolves duplicated name duplication?
是的。引用N4140的相关部分:
§3.3.1 [basic.scope.declarative] / 4
Given a set of declarations in a single declarative region, each of
which specifies the same unqualified name,
- they shall all refer to the same entity, or all refer to functions and function templates; or
- exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all
refer to the same variable or enumerator, or all refer to functions
and function templates; in this case the class name or enumeration
name is hidden. [ Note: A namespace name or a class template name
must be unique in its declarative region. —end note ]
我想你不小心让你的例子为你工作,因为你重复了包含守卫。我能够从问题中为 class S
重现 "preference":
#ifndef FOO
#define FOO
struct S{
static int f(){return 1;}
};
#endif
#ifndef FOO
#define FOO
namespace S{
static int f(){return 2;}
}
#endif
#include<stdio.h>
int main()
{
int i = S::f();
printf("%d\n",i);
return 0;
}
我正在测试 C++ 的名称查找规则。我有一个包含 3 个文件的简单程序:
$cat testns01.h
struct S{
static int f(){return 1;}
};
$cat testns02.h
namespace S{
static int f(){return 2;}
}
$cat testns3.cpp
#include "testns02.h" // namespace
#include "testns01.h" // struct
#include<stdio.h>
int main()
{
int i = S::f();
printf("%d\n",i);
return 0;
}
如果我编译 运行,我得到:
$g++ testns3.cpp && ./a.out
1
好的,我有 3 个问题:
- name "S" class name 和 namespace 之间的重复不冲突?
- 当两者都具有名称 "S" 时,似乎 "struct S" 具有更高的优先级
如果我注释掉#include "testns01.h"这一行,程序会打印2,还是可以的。所以我的第三个问题是:
- c++ 标准是否讨论名称查找如何解决名称重复问题?
- name "S" duplication between class name and namespace doesn't conflict?
他们有。
- When both has name "S", seems "struct S" has higher priority
没有。 (往下看)
- Does c++ stardard talke about how name lookup resolves duplicated name duplication?
是的。引用N4140的相关部分:
§3.3.1 [basic.scope.declarative] / 4
Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,
- they shall all refer to the same entity, or all refer to functions and function templates; or
- exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden. [ Note: A namespace name or a class template name must be unique in its declarative region. —end note ]
我想你不小心让你的例子为你工作,因为你重复了包含守卫。我能够从问题中为 class S
重现 "preference":
#ifndef FOO
#define FOO
struct S{
static int f(){return 1;}
};
#endif
#ifndef FOO
#define FOO
namespace S{
static int f(){return 2;}
}
#endif
#include<stdio.h>
int main()
{
int i = S::f();
printf("%d\n",i);
return 0;
}