There is a standardized method, but not many use it:
class X { T data; public: template<typename autoconst> friend auto get_data(autoconst& that) -> decltype(that.data) {
It is called as get_data(x) , not x.get_data() , but one implementation serves both to use const and not const , without casting or other methods without types.
You can also have member functions to enable member invocation syntax. This will require options const and non const , but not duplication of the process that.data , since both can delegate the template to a friend.
A more complete example:
template<typename T> class HyperMatrix { int rows, cols, planes; T* data; public: template<typename ThisType> friend auto at(ThisType& that, int const r, int const c, int const p) -> decltype(*(that.data)) {
An example without a trivial workaround:
template<typename T> class BalancedJumpTable { public: template<typename ThisType, typename Functor> friend auto for_each(ThisType& that, Functor f) {
Ben voigt
source share