Type alias for auto
Asked Answered
L

1

5

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?

Loris answered 16/7, 2020 at 11:43 Comment(7)
Don't. Code is predominantly meant to be read by other people. Creating your own idiosyncratic name for something, just because you like it better in another language, is going to make other C++ programmers less able to understand your program. It's almost always better to be idiomatic.Cannibalism
Downvote a question but not an idea guys. Write an answer if you want to address the idea.Uhland
@AsteroidsWithWings Yes, you are right. Though it's not suggested, but it's allowedDiva
@Diva It's true that you can vote however you like. But the system works best if everybody follows the spirit of it.Uhland
@AsteroidsWithWings yes I know. I didn't vote-down. My comment is to express my agreement with your comment. The question itself is a good idea I think and also it is a "nice to have" question.Diva
@arsdever: 👍👍👍👍Uhland
@Diva I didn't downvote, but I don't see much value in this question either: The OP has not done any research into what 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
U
16

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.

Uhland answered 16/7, 2020 at 11:45 Comment(1)
I was just curious if it's possible w/o macro, would not use it anyway.Loris

© 2022 - 2024 — McMap. All rights reserved.