0

Why isn't the following valid?

bool trigger(t_evt evt) const {
    std::shared_ptr<I> ptr = this->instance.lock();

    if (!ptr) {
        return false;
    }

    (ptr->*f)(evt); // -> causes a compilation error

    return true;
}

The error in english is something like that (I don't get it in english):

'->*' : improper usage, left operand of type 'std::shared_ptr<ListenerCardsChange>'

f is a pointer to a member function.

Note that (*ptr.*f)(evt); works fine.

Serge Profafilecebook
  • 1,165
  • 1
  • 14
  • 32

1 Answers1

1

That's because shared_ptr<T>, and also unique_ptr<T, D>, do not overload operator->*. That's arguably a defect, but I haven't thought about it much and am not 100% sure that there isn't some reason for that.

Use the (*ptr.*f)(evt) syntax.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084