0

Code in header:

extern const char* const foo;

Code in source:

const auto foo = "bar";

Visual studio 2015 producess the following error:

Error C2040 'foo': 'const auto' differs in levels of indirection from 'const char *const '

My embedded compiler (uVision from Keil) has no problem with this code, neither does Clang (coliru). My question is, is this a bug in the VS compiler or is there a problem in my code?

rozina
  • 4,120
  • 27
  • 49

2 Answers2

0

I think your auto is not const enough - it's deduced as const char * (which is the type of a string literal), and you need const char * const. I don't think it's a compiler bug.

I would expect that this code to throw an error with in GCC and clang when compiled with -pedantic-errors.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
0

The definition shall follow the declaration (omitting extern): const char* const foo = "bar";

dmi
  • 1,424
  • 1
  • 9
  • 9