Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is template argument deduction failing for pointer-to-member-function?

With g++ 5.4, this

struct B {
    void f() {}
}; 

struct D : public B {
    void g() {}
};

template <class T>
void foo(void (T::*)(), void (T::*)())
{}

int main()
{
    foo(&D::f, &D::g);
}

fails due to "deduced conflicting types for parameter ‘T’ (‘B’ and ‘D’)". Why isn't T deduced as D, being an exact match?

like image 451
fizzer Avatar asked Nov 09 '17 12:11

fizzer


2 Answers

The type of &D::f would be void ( B::* )(void)

static_assert(::std::is_same<void ( B::* )(void), decltype(&D::f)>::value, "");
static_assert(::std::is_same<void ( D::* )(void), decltype(&D::f)>::value, ""); // error
static_assert(::std::is_same<void ( D::* )(void), decltype(&D::g)>::value, "");

The rationale behind this is that otherwise you won't be able to assign a value of &D::f to a variable of void ( B::* )(void) type without a cast even though f is a member of B or compare &D::f == &B::f.

As a workaround you can perform a static_cast:

foo(static_cast<void (D::*)(void)>(&D::f), &D::g);
like image 74
user7860670 Avatar answered Nov 11 '22 05:11

user7860670


In addition to VTT's excellent demonstration. The standard text in question, I believe, is at [expr.unary.op]/3, emphasis mine:

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static or variant member m of some class C with type T, the result has type “pointer to member of class C of type T” and is a prvalue designating C​::​m.

The qualified id you used is D::f, but it names a member function of B (I can bring up the lookup rules if you want). So the class type C in the above paragraph, is B. The type therefore resolves to void ( B::* )(void).

like image 42
StoryTeller - Unslander Monica Avatar answered Nov 11 '22 04:11

StoryTeller - Unslander Monica