Search code examples
c++language-lawyerautovariable-declaration

Is it well-formed, if I redefine a variable as auto, and the deduced type is the same?


Look at this snippet:

int a;
extern int b;
auto b = a;

Is it well-formed? Clang successfully compiles it, but GCC and MSVC don't.

(This issue has come up when I answered How to declare and define a static member with deduced type?)


Solution

  • Tl;DR;

    clang is correct, the logic is that this is allowed by [dcl.spec.auto] and to restrict this for deduced return types [dcl.spec.auto]p11 was added otherwise there is no restriction and therefore this is not restricted for the variables case.

    See my more complete answer in the duplicate