Search code examples
c++autotype-alias

Type alias for auto


Is it possible to create a type alias for auto? I've tried both using var = auto and typedef auto var, and both throw an error that it's not allowed. So is there a way?


Solution

  • No.

    auto is not a type.

    It is a keyword that triggers type deduction.

    For it to work, there needs to be some information for it to deduce from! That's why it works in a declaration with an initialiser.

    If you really want to be able to write var in your code to get the same effect, like in (say) JavaScript, you could write a macro:

    #define var auto
    

    This will make the "word" var behave just like the "word" auto.

    But please, please, please don't do that. It will only make your code harder to understand by C++ developers, without actually providing any tangible benefit to anybody in return.