F
Firstname Lastname
In a function call, for example function(Class(), &Class::memfunc), my
compiler cannot match the 2nd argument when: (1) Class is a template
parameter, and (2) the base class of Class defines memfunc but Class itself
does not.
Is this the correct behavior? Second, are there any workarounds?
Consider the following program, which has a compile error in the last line
silly(b, &B::f).
#include <iostream>
#include <typeinfo>
using std::cout;
struct A
{
A() { }
void f() const { cout << "A::f()\n"; }
};
struct B : A
{
};
template <class T>
void silly(const T& t, void (T::* ptr)() const)
{
(t.*ptr)();
}
int main(int argc, char const *const * argv, char const *const * env)
{
const A a;
silly(a, &A::f);
const B b;
silly(b, &B::f); // compile error: can't match silly(const B&, void
(A::*)())
}
If we replace the
template <class T>
void silly(const T& t, void (T::* ptr)() const)
with
void silly(const B& t, void (B::* ptr)() const)
then the last line compiles fine.
compiler cannot match the 2nd argument when: (1) Class is a template
parameter, and (2) the base class of Class defines memfunc but Class itself
does not.
Is this the correct behavior? Second, are there any workarounds?
Consider the following program, which has a compile error in the last line
silly(b, &B::f).
#include <iostream>
#include <typeinfo>
using std::cout;
struct A
{
A() { }
void f() const { cout << "A::f()\n"; }
};
struct B : A
{
};
template <class T>
void silly(const T& t, void (T::* ptr)() const)
{
(t.*ptr)();
}
int main(int argc, char const *const * argv, char const *const * env)
{
const A a;
silly(a, &A::f);
const B b;
silly(b, &B::f); // compile error: can't match silly(const B&, void
(A::*)())
}
If we replace the
template <class T>
void silly(const T& t, void (T::* ptr)() const)
with
void silly(const B& t, void (B::* ptr)() const)
then the last line compiles fine.