c++ - Another BUG of VC++ 2010? About declaring a constant REFERENCE in a header -


several lines of code worth thousand words:

i have 3 simple files: header.h, main.cpp, other.cpp

// header.h    #pragma once  inline const int& getconst() {     static int n = 0;     return n; }  const int& r = getconst();  // main.cpp    #include "header.h"  int main() {     return 0; }  // other.cpp    #include "header.h" 

when compiling simplest project, vc++ 2010 complains follows:

clcompile:   other.cpp   main.cpp   generating code...   other.obj : error lnk2005: "int const & const r" (?r@@3abhb) defined in main.obj d:\test\debug\bug.exe : fatal error lnk1169: 1 or more multiply defined symbols found  build failed.  time elapsed 00:00:00.29 ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

i sure bug of vc++ 2010, because of following 2 references:

1, c++ standard says: (at page 140 of n3126)

"objects declared const , not explicitly declared extern have internal linkage."

2, msdn says: (at: http://msdn.microsoft.com/en-us/library/357syhfh(vs.80).aspx)

"in c, constant values default external linkage, can appear in source files. in c++, constant values default internal linkage, allows them appear in header files.

the const keyword can used in pointer declarations."

the paragraph cite c++ standard reads (c++03 7.1.1/6):

objects declared const , not explicitly declared extern have internal linkage.

you have not declared object. have declared reference. reference not object. said, 3.5/3 says:

a name having namespace scope has internal linkage if name of object or reference explicitly declared const , neither explicitly declared extern nor declared have external linkage

however, 8.3.2/1 says:

cv-qualified references ill-formed

so, while const-qualified reference have internal linkage, it's not possible const-qualify reference.

the reference in sample program not const-qualified, it's reference const-qualified int.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -