3

I'm guessing this just isn't legal in C++, but I thought I'd ask, given a struct that I don't own:

struct foo {
    int x;
    int y;
    int z;
};

I want to write a non-member subscript operator for it:

int& operator [](foo& lhs, const std::size_t rhs) {
    switch(rhs) {
    case 0U:
        return lhs.x;
    case 1U:
        return lhs.y;
    case 2U:
        return lhs.z;
    default:
        return *(&(lhs.z) + rhs - 2U);
    }
}

I'm getting this error:

error: int& operator[](foo&, std::size_t) must be a nonstatic member function

Yakk - Adam Nevraumont
  • 235,777
  • 25
  • 285
  • 465
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241

2 Answers2

4
struct foo {
    int x;
    int y;
    int z;

  int& operator [](const std::size_t rhs) & {
    switch(rhs) {
      case 0U:
        return this->x;
      case 1U:
        return this->y;
      case 2U:
        return this->z;
      default:
        return *(&(this->z) + rhs - 2U);
    }
  }
};

not all operators can be overloaded as free functions.

Not the standard, but clearly written over at cppreference, the operators [], =, -> and () must be non-static member functions.

If you can do wrap(f)[2] you can get it to work. But there is no way to get it to work strait on a foo instance.

template<class T>
struct index_wrap_t {
  T t;
  template<class Rhs>
  decltype(auto) operator[](Rhs&& rhs)& {
    return operator_index( *this, std::forward<Rhs>(rhs) );
  }
  template<class Rhs>
  decltype(auto) operator[](Rhs&& rhs)&& {
    return operator_index( std::move(*this), std::forward<Rhs>(rhs) );
  }
  template<class Rhs>
  decltype(auto) operator[](Rhs&& rhs) const& {
    return operator_index( *this, std::forward<Rhs>(rhs) );
  }
  template<class Rhs>
  decltype(auto) operator[](Rhs&& rhs) const&& {
    return operator_index( std::move(*this), std::forward<Rhs>(rhs) );
  }
};

template<class T>
index_wrap_t<T> index( T&& t ) { return {std::forward<T>(t)}; }

then you can do this:

int& operator_index( foo& lhs, std::size_t rhs ) {
  // your body goes here
}
foo f;
index(f)[1] = 2;

and it works.

index_wrap_t forwards [] to a free call to operator_index that does ADL.

Yakk - Adam Nevraumont
  • 235,777
  • 25
  • 285
  • 465
  • Yeah... :( I don't own `foo` :( – Jonathan Mee Jun 14 '19 at 14:56
  • 1
    @JonathanMee: Can you inherit from `foo`? – Bathsheba Jun 14 '19 at 14:59
  • @Bathsheba If `foo` is a non-abstract class (that others use, i.e. not an implementation detail) then doing that is somewhere between discouraged and awful. – Max Langhof Jun 14 '19 at 15:01
  • @MaxLanghof: As a rule of thumb I'm inclined to agree; really depends on the use case and full details, probably beyond the scope of an answer. – Bathsheba Jun 14 '19 at 15:02
  • @Bathsheba Hmmm... it is a POD class, and I can inherit from it... Hmmm – Jonathan Mee Jun 14 '19 at 15:04
  • @Bathsheba So say that I write `struct foo_plus : foo` which has my subscript operator. How can I use that operator on a `foo` object? Are we talking about me `reinterpret_casting` from a `foo` to a `foo_plus`? I mean that sounds *really* illegal. – Jonathan Mee Jun 14 '19 at 15:16
  • @JonathanMee: I'd need to see a detailed use case. An alternative would be to build some kind of enveloping class for `foo`, with appropriate conversion operators defined. – Bathsheba Jun 14 '19 at 15:20
1

You can have a wrapper class and function:

struct foo_wrapper {
    foo &ref;

    foo_wrapper( foo &f ) : ref( f ) {}
    int &operator[]( std::size_t rhs ) {
       switch(rhs) {
       case 0U:
           return ref.x;
       case 1U:
           return ref.y;
       case 2U:
           return ref.z;
       default:
           return *(&(ref.z) + rhs - 2U);
        }
    }    
};

foo_wrapper wrap( foo &ff )
{
    return foo_wrapper( ff );
}

foo f;
wrap( f )[1] = 123;

Live example You may need this helper function as directly writing:

foo_wrapper( f )[2] = 0; 

would lead to compilation error (re-declaration of f)

Slava
  • 40,641
  • 1
  • 38
  • 81