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?
Type alias for auto
Asked Answered
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.
I was just curious if it's possible w/o macro, would not use it anyway. –
Loris
© 2022 - 2024 — McMap. All rights reserved.
auto
actually is in C++, or what it's supposed to do. This could even be used to justify the closing of this question. I agree, though, that it might be helpful to have this question around, if only to point other people to it who haven't done the research either. – Relent