Search code examples
c++auto

Implicit auto type specifier


I was wondering why it is not possible to omit the auto keyword in some/all cases entirely, e.g.

int main()
{
  [](auto x){}(10); // why this?
  [](x){}(10); // and not this?

  auto x = 10;
  x = 10;
}

Is there a problem with ambiguity or something similiar? Or was it simple a design choice?


Solution

  • Consider this:

     struct x {};
    
     [](x){}(10);
    

    Is that a lambda with an unnamed argument of type x (as per the current language spec) or is it am argument named x of deduced type (as per your suggestion)? Your suggested syntax is ambiguous with the pre-existing syntax of function parameter declarations.


    x = 10;
    

    This is even more problematic because it is indistinguishable from assignment. Someone writing this might be attempting to define a variable (your suggestion), but it can just as well be assignment of an existing variable depending on context. C++ has too much syntactical ambiguity (for the programmer) as it is. We should avoid adding more.