I'm reading through Scott Meyers effective C++ book and he recommends for operators making both const and non-const versions, where the non-const just calls the const version will some type stripping to reduce code repetition.
His example is something like this:
const char& CallConstTest::operator[](size_t index) const
{
//Some safety code
return m_buffer[index];
}
char& CallConstTest::operator[](size_t index)
{
return const_cast<char&>(static_cast<const CallConstTest&>(*this)[index]);
}
Now I agree with doing this but I really don't like those casts for readability reasons, so added some template functions that work to try and increase readability as shown below:
template<typename baseType>
const baseType& AddConst(baseType& base){
return static_cast<const baseType&>(base);
}
template<typename baseType>
baseType& RemoveConst(const baseType& base) {
return const_cast<baseType&>(base);
}
char& CallConstTest::operator[](size_t index)
{
return RemoveConst(AddConst(*this)[index]);
}
But then thought to myself, everyone will tell me to use a standard library to do this so it's uniform with any codebases. I found std::add_const and std::remove_const but the add explicitely says "(unless T is a function, a reference, or already has this cv-qualifier)" so that's a no go, and I cannot get std::remove_const to be happy, probably for similar reasons.
Questions
- Is there a commonly used function that everyone already uses for this
- If you argue that const_cast<type&>(static_cast<const type&> is actually the correct answer, please explain why