visual c++ - Is this a BUG of VC++ 2010? About declaring a constant object in a header -


several lines of code worth thousand words:

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

==== code begin ====  // header.h     #pragma once    const void* p = 0;  // main.cpp    #include "header.h"  int main() {     return 0; }  // other.cpp    #include "header.h" ==== code end ==== 

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

clcompile:   other.cpp   main.cpp   generating code... other.obj : error lnk2005: "void const * const p" (?p@@3pbxb) 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. the c++ standard says: (at page 140 of n3126)

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

  2. the msdn says:

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

const void *p = 0; defines p pointer to const void, not define p const @ all. since p not const object, rule giving internal linkage not apply, has external linkage.

void *const p = 0; define p const pointer. void const * const p define p const pointer const void.


Comments

Popular posts from this blog

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

html - Instapaper-like algorithm -

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