patterncppMinor
Const by default
Viewed 0 times
defaultconststackoverflow
Problem
Being a C++ developer, but also having been influenced by functional languages, I strongly believe that
C++ does provide a
I tried to find a different solution, and I would like to have some experts review my attempt.
This example shows the idiomatic C++14 of initializing a complex
With this macro, the previous code can be rewritten as:
This one is more controversial. I think that
So, the philosophy is, for any given class
const should be the default for the vast majority of all variables when they are created and passed as arguments.C++ does provide a
const keyword, but it is not "by default". Many people have asked questions about this, and the classic answer it "you would need to invent another language, but C++ is here to stay".I tried to find a different solution, and I would like to have some experts review my attempt.
const_initializeconst_initialize aims to make it easy to instantiate a const variable, even when its initialization is complex. This is inspired by an article from Herb Sutter.This example shows the idiomatic C++14 of initializing a complex
const variable through a lambda://construct and initialize vec of type const std::vector
const auto vec = [&]() { //define a lambda that initializes the vector
std::vector _vec;
for (unsigned i = 0; i < 10; i++)
_vec.push_back(i);
return _vec;
}(); //run the lambda immediatelyconst_initialize is an attempt to make this code shorter and easier to read. With this macro, the previous code can be rewritten as:
const_initialize( std::vector, vec, // here, we define the type and name of the const variable.
for (unsigned i = 0; i < 10; i++) //initialization code
vec.push_back(i);
);const_initialize:#define const_initialize(type, name, initialization_code) \
const auto name = [ & ]() \
{ \
type name; \
initialization_code; \
return name; \
}();const_typedefsThis one is more controversial. I think that
const ought to be the default, and we should not spend time reading and writing const in any given source code.So, the philosophy is, for any given class
Footo make its const variants very easy and fast to read and wSolution
Ask yourself this: "Do I really need a macro?" Then clear your mind of all thoughts. Think about puppies. Meditate. After meditating for a while and all of your troubles have washed away, ask again: "Do I really need a macro?" If your answer is yes, you probably need a vacation. Maybe somewhere in the Bahamas.
In times like these I like to bring up some salient points:
-
Herb Sutter is not a God, although on occasion he's deserving of the title
-
Macros are almost always bad. They obfuscate code, create horrible compiler errors, and are difficult to maintain
-
C++11 features are cool, but should only be used where appropriate, rather than shoehorned into every nook and cranny possible
-
So what is the solution:
-
Recognize that this cute trick is only applicable to this one scenario, and shouldn't be generalized without good reason
-
Lambdas are just syntactic sugar for functors. The usual caveats apply
Future maintainers will thank you.
Now, some commenters on the article believe that the original asker is actually talking about lazily initializing a
In times like these I like to bring up some salient points:
-
Herb Sutter is not a God, although on occasion he's deserving of the title
-
Macros are almost always bad. They obfuscate code, create horrible compiler errors, and are difficult to maintain
-
C++11 features are cool, but should only be used where appropriate, rather than shoehorned into every nook and cranny possible
-
const correctness does not imply "use const everywhere"So what is the solution:
-
Recognize that this cute trick is only applicable to this one scenario, and shouldn't be generalized without good reason
-
Lambdas are just syntactic sugar for functors. The usual caveats apply
Future maintainers will thank you.
Now, some commenters on the article believe that the original asker is actually talking about lazily initializing a
const variable. In that case, there's something that's much more terser and clearer:const int i = (someCondition) ? calc() : default_value;Code Snippets
const int i = (someCondition) ? calc() : default_value;Context
StackExchange Code Review Q#106074, answer score: 3
Revisions (0)
No revisions yet.