Search code examples
c++c++14function-pointersboost-hana

Taking the address of boost::hana::partial::operator()


Suppose I have a lambda that does something:

auto thing = [](int x){ /* Stuff */ };

I want to save off the value "x" and call it later so I do:

auto other = boost::hana::partial(thing, 42);

Now, because I want to do some type erasure on this I want to take the address of the operator().....so I try to do this:

using type = decltype(other);
void (type::*ptr)(void) = &type::operator();

Clang complains, that the other object does not a sufficient function that matches the requirements: godbolt

It appears that the partial type is returning a reference (to void?)......why does this not work?


Solution

  • It (the operator()) has & const& && etc overloads; your member function pointer does not qualify *this by const or r/l value-ness. So does not match.

    Add & or const& or && or const&& before = on the line that fails to compile, and it will compile.

    Here is a [MCVE]:

    struct foo {
        void bar()&{}
    };
    
    int main(){
        auto p = &foo::bar;
        void(foo::*p2)() = p; // lacks &
    }