In the following class, wrapper takes a pointer to an arbitrary const method and returns the result of a call to that method with const removed. This can be used to generate the corresponding non-const method...
struct C {
int x[10];
int const& get(int i) const { return x[i]; }
int const& getr(int const& i) const { return x[i]; }
template<typename T, typename... Ts>
auto& wrapper(T const& (C::*f)(Ts...) const, Ts... args) {
return const_cast<T&>((this->*f)(args...));
}
int& get(int i) { return wrapper(&C::get, i); }
int& getr(int const& i) { return wrapper(&C::getr, i); }
};
almost.
The problem is that the final method getr() cannot be compiled, because the argument list passed to wrapper() doesn't imply pass-by-reference. By the time we get inside wrapper() the compiler is looking for a pass-by-value version of getr().
Is there a trick to this?