Skip to content

Instantly share code, notes, and snippets.

@addam
Created May 25, 2016 10:04
Show Gist options
  • Save addam/63fe5b36043a29fbdcd64a59e4b085bb to your computer and use it in GitHub Desktop.
Save addam/63fe5b36043a29fbdcd64a59e4b085bb to your computer and use it in GitHub Desktop.
Scott Meyers' suggestion to aviod const/nonconst method duplication
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