Q1. No. Q2. By using them as little as possible. Q3. Quite bad. C++ programmers are used to a given syntax. Q4. I don't see what you want there, or why.
Introducing synonyms for "auto"
-
17-06-2023 - |
문제
Introducing synonyms for "auto"
I'd like to use "Almost always auto" style with C++, but in a more descriptive form, reflecting the semantics of initialization.
To do it I want to introduce synonyms for 4 distinct uses of auto.
The general idea can be understood from the example:
#define Val const auto // initialize (immutable) value
#define MVal auto // initialize mutable value
#define Ref const auto& // initialize (immutable) reference
#define MRef auto&& // initialize mutable reference
Val num = 2 + 2;
MVal count = 0;
++count;
Ref widget = GetWidget();
MRef gadget = GetGadget();
gadget.ChangeState();
The several questions arise:
Q1: Can the synonyms be introduced without macros?
Q2: If macros are inevitable, how do I mitigate the harm from using them (assuming the synonyms should be really short)?
Q3 (primarily opinion based): Is it a good idea at all (compared to naked AAA-style) from a readability/style point of view?
Q4 (insane): Are there any fundamental reasons to disallow auto in typedefs (implementability, bad interaction with other features)? (Assuming "typedef auto" is available the problem can be solved trivially)
해결책
다른 팁
Q1. yes synonyms can be introduced in this way. Q2. Use ALLUPPERCASE to follow the convention. Q3. But generally it is not a good idea and I would find more readable const auto
than VAL
Q4. I think it is not supported because typedef is a defined type, and const auto
is not.