jeudi 2 juin 2016

Linking error due to constness of the pointer array in C++03/C++11

This issue is reproducible in the g++ prior to -std=c++14. A linking error is generated due to highlighted const shown in below codes. It disappears, if the RHS const is removed.

/* main.cpp */
const char* const arr[2] = {"Hello", "World"};
//          ^^^^^
int main () {}

and

/* foo.cpp */
extern const char* const arr[2];
//                 ^^^^^
void foo () {
  const char* p = arr[1];
}

While compiling: g++ [-std=c++11] main.cpp foo.cpp, it gives following linking error:

In function `foo()': undefined reference to `arr'  

Is it a bug or a language limitation?

Aucun commentaire:

Enregistrer un commentaire