patterncppMinor
A less verbose const_cast
Viewed 0 times
verboselessconst_cast
Problem
Motivation
Nobody knows what most of the C++ programmers do but so far I only ever had one use for
IMHO, there is no gain in readability when repeating the type.
Enter my proposal:
unconst_cast
We can write a template function
Our code from above would become:
Other pros:
Review Goals
const_cast is a beast that I seldomly encounter. But when I do so it is mostly a bad experience.Nobody knows what most of the C++ programmers do but so far I only ever had one use for
const_cast: to cast away a const from a type. For this task const_cast is too verbose:const std::vector constVectorOfInts = {/*...*/};
// never do this in real code!
auto &mutableVectorOfInts = const_cast>(constVectorOfInts);IMHO, there is no gain in readability when repeating the type.
Enter my proposal:
unconst_cast
We can write a template function
unconst_cast that solves only the problem of removing const./**
* @brief Returns a mutable reference to the same object
*/
template T &unconst_cast(const T &v) { return const_cast(v); }
/**
* @brief Returns a mutable pointer to the same object
*/
template T *unconst_cast(const T *v) { return const_cast(v); }
int main() {
int i;
const int *constPointerToI = &i;
int *unconstPointerToI = unconst_cast(constPointerToI);
unconstPointerToI = unconst_cast(&i);
int &unconstReferenceToI = unconst_cast(*constPointerToI);
int &unconstReferenceToI2 = unconst_cast(i);
}Our code from above would become:
auto &mutableVectorOfInts = unconst_cast(constVectorOfInts);Other pros:
- can overload for other types (in contrast to
const_cast), e.g. for own iterators (mutableIterator = unconst_cast(constIterator))
- we find usages when string searching for "const_cast"
Review Goals
- Is this a bad idea?
- Is my implementation correct?
- Is the naming alright/understandable?
Solution
•Is this a bad idea?
It is unnecessary.
Nobody knows what most of the C++ programmers do but so far I only ever had one use for const_cast: to cast away a const from a type.
You can also use it to partially take away constness:
The gain in readability comes from the result type being explicitly specified.
If you have APIs that only work for non-const pointers when they should work for const (this is the most legitimate use for const_cast) you should wrap them and centralize/localize/hide the call to const_cast.
If you find yourself writing const_cast calls again and again, the problem is not poor readability of the code, but bad API design.
It is unnecessary.
Nobody knows what most of the C++ programmers do but so far I only ever had one use for const_cast: to cast away a const from a type.
You can also use it to partially take away constness:
int do_things(const char* p);
const char* const xyz = "abc";
do_things( const_cast(xyz) );The gain in readability comes from the result type being explicitly specified.
If you have APIs that only work for non-const pointers when they should work for const (this is the most legitimate use for const_cast) you should wrap them and centralize/localize/hide the call to const_cast.
If you find yourself writing const_cast calls again and again, the problem is not poor readability of the code, but bad API design.
Code Snippets
int do_things(const char* p);
const char* const xyz = "abc";
do_things( const_cast<const char*>(xyz) );Context
StackExchange Code Review Q#102587, answer score: 5
Revisions (0)
No revisions yet.