Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 353
DeNice Avatar asked Jul 16 '20 11:07

DeNice


Video Answer


1 Answers

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.

like image 55
Asteroids With Wings Avatar answered Oct 24 '22 19:10

Asteroids With Wings