Why ampersand is needed to refer to a call operator

I thought the C ++ specification says that the ampersand before the function is not needed when referring to the function, i.e.

void bar();
void foo(void(*bar)());

foo(bar);
foo(&bar);  // Same as above.

However, I found a case where this is not true. I tried to create a specialized lambda specialization (for only one argument) so that I could access the return argument and input lambda argument types.

// The ampersand in front of 'Fn::operator()' is necessary to make
// this code work.
template <typename Lambda>
struct Signature : public Signature<decltype(&Fn::operator())> {};

template <typename ClassT, typename RetT, typename ArgT>
struct Signature<RetT(ClassT::*)(ArgT) const> {
  using ReturnType = RetT;
  using ArgumentType = ArgT;
};

Without an ampersand, clang complains

error: call to non-static member function without an object argument
struct Signature : public Signature<decltype(Fn::operator())> {};
                                             ~~~~^~~~~~~~

I got the code to work, but I would like to understand why it works. Why is an ampersand needed here?

+4
source share
2 answers

-, & (). / lambdas, - ++.

, . ++. , C , & .

+3

- - . , - -. , - -. , &.

, , - , , :

int f();
// ...
if (f) { // always true
}

C, , .

0

Source: https://habr.com/ru/post/1621048/


All Articles