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 |
|
|
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.
|