由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ Q07: unnamed namespace
相关主题
C++ Q14: unnamed namespacedefault linkage for const global variable in C/C++
C++ 全局变量是怎么回事?What're the three types of memory allocated for C++ variables?
c++ does not check const for extern variable?static variable in template header
问一个问题,面试被问倒了也问个template 的问题(C++)
c++ class definitioninline到底能省多少时间?
这段code有啥问题?C++: Static initialization dependency
C++ Q01: private inheritance.c++中的inline 函数是做什么的?
请教: 用stable_sort 在VC++下通过但在g++下通不过c++ 弱问题:static const char* const 这两个const 分别是什么意思?
相关话题的讨论汇总
话题: namespace话题: linkage话题: c++话题: declared话题: external
进入Programming版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
namespace { int i; }
Which statement about the sample code above is true?
a) "i" has external linkage.
b) The code is equivalent to: static int i;.
c) "i" is visible to other compilation units.
d) "i" is only visible within the namespace.
E*U
发帖数: 2028
2
b

【在 c**********e 的大作中提到】
: namespace { int i; }
: Which statement about the sample code above is true?
: a) "i" has external linkage.
: b) The code is equivalent to: static int i;.
: c) "i" is visible to other compilation units.
: d) "i" is only visible within the namespace.

c**********e
发帖数: 2007
3
not right.

【在 E*U 的大作中提到】
: b
E*U
发帖数: 2028
4
a

【在 c**********e 的大作中提到】
: not right.
r****t
发帖数: 10904
5
别逗了,本版成了 GRE 版了。。。

【在 E*U 的大作中提到】
: a
d****p
发帖数: 685
6
I think he is right.

【在 c**********e 的大作中提到】
: not right.
E*U
发帖数: 2028
7
en. I am puzzled
Internal linkage
The following kinds of identifiers have internal linkage:
* Objects, references, or functions explicitly declared static
* Objects or references declared in namespace scope (or global scope in
C) with the specifier const and neither explicitly declared extern, nor
previously declared to have external linkage
* Data members of an anonymous union
* C++ Function templates explicitly declared static
* C++ Identifiers declared in the unnamed names

【在 d****p 的大作中提到】
: I think he is right.
E*U
发帖数: 2028
8
from another place
Note that using static for internal linkage is deprecated since it can't be
used on classes and you should use anonymous namespaces which although they
have external linkage are unreachable from other translation units.

in

【在 E*U 的大作中提到】
: en. I am puzzled
: Internal linkage
: The following kinds of identifiers have internal linkage:
: * Objects, references, or functions explicitly declared static
: * Objects or references declared in namespace scope (or global scope in
: C) with the specifier const and neither explicitly declared extern, nor
: previously declared to have external linkage
: * Data members of an anonymous union
: * C++ Function templates explicitly declared static
: * C++ Identifiers declared in the unnamed names

I*****y
发帖数: 602
9
agree. b should be the answer.

【在 d****p 的大作中提到】
: I think he is right.
k*******d
发帖数: 701
10
经测试b是正确的
#include
namespace {int i;}
void f(){i++;
std::cout<<"i in f:"< }
int main(){
f();
i++;
std::cout<<"i in main:"< }
输出结果为
i in f:1
in in main:2
相关主题
这段code有啥问题?default linkage for const global variable in C/C++
C++ Q01: private inheritance.What're the three types of memory allocated for C++ variables?
请教: 用stable_sort 在VC++下通过但在g++下通不过static variable in template header
进入Programming版参与讨论
s******e
发帖数: 431
11
你这个代码只能证明D不对。你还需要另外一个cpp文件。

【在 k*******d 的大作中提到】
: 经测试b是正确的
: #include
: namespace {int i;}
: void f(){i++;
: std::cout<<"i in f:"<: }
: int main(){
: f();
: i++;
: std::cout<<"i in main:"<
w******g
发帖数: 67
12
B.
reference: C++ primer plus
X****r
发帖数: 3557
13
Where is this list come from? Reference?

in

【在 E*U 的大作中提到】
: en. I am puzzled
: Internal linkage
: The following kinds of identifiers have internal linkage:
: * Objects, references, or functions explicitly declared static
: * Objects or references declared in namespace scope (or global scope in
: C) with the specifier const and neither explicitly declared extern, nor
: previously declared to have external linkage
: * Data members of an anonymous union
: * C++ Function templates explicitly declared static
: * C++ Identifiers declared in the unnamed names

c**********e
发帖数: 2007
14
Thank you guys for the discussion. evu and decamp are right, based on the
given answer. Testing is a good way for exploration. But some times (like this example) it does not tell the correct answer. Sometimes, testing is not exhaustive. In a compiling system I use, sometimes it gives results inconsistent to the Standard.
This question is intended to illustrate the use of anonymous namespaces.
Here, the wrong answers have as much exposition value as the correct answer.

a) "i" has external link
a*******s
发帖数: 79
15
是啊,这个写法的目的就是不要跨.cpp文件好像

【在 I*****y 的大作中提到】
: agree. b should be the answer.
1 (共1页)
进入Programming版参与讨论
相关主题
c++ 弱问题:static const char* const 这两个const 分别是什么意思?c++ class definition
这个 perl 输出的数字为什么自动加了换行?谢谢!这段code有啥问题?
求助一个std::map的怪问题C++ Q01: private inheritance.
question on reserve() in vector container.请教: 用stable_sort 在VC++下通过但在g++下通不过
C++ Q14: unnamed namespacedefault linkage for const global variable in C/C++
C++ 全局变量是怎么回事?What're the three types of memory allocated for C++ variables?
c++ does not check const for extern variable?static variable in template header
问一个问题,面试被问倒了也问个template 的问题(C++)
相关话题的讨论汇总
话题: namespace话题: linkage话题: c++话题: declared话题: external