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);
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.
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?
source
share