Created
May 25, 2016 10:04
-
-
Save addam/63fe5b36043a29fbdcd64a59e4b085bb to your computer and use it in GitHub Desktop.
Scott Meyers' suggestion to aviod const/nonconst method duplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Result, typename M, typename A> | |
Result& nonconst_call(const M& m, A& argument) | |
{ | |
return const_cast<Result&>(static_cast<const M&>(m)(argument)); | |
} | |
Value const& Container::operator () (int index) const | |
{ | |
return data[index]; | |
} | |
Value& Container::operator () (int index) | |
{ | |
return nonconst_call<Value>(*this, index); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment