In the following code:
#include <iostream>
struct Base {
virtual ~Base() = default;
template <typename T, typename... Args> void helper (void (T::*)(Args..., int), Args...);
void bar (int n) {std::cout << "bar " << n << std::endl;}
};
struct Derived : Base {
void baz (double d, int n) {std::cout << "baz " << d << ' ' << n << std::endl;}
};
template <typename T, typename... Args>
void Base::helper (void (T::*f)(Args..., int), Args... args) {
// A bunch on lines here (hence the motivation for the helper function)
for (int n = 0; n < 5; n++)
(dynamic_cast<T*>(this)->*f)(args..., n);
// ...
}
int main() {
Base b;
Derived d;
b.helper(&Base::bar); // GCC 4.8.1 will accept this, Visual Studio 2013 won't.
d.helper<Derived, double>(&Derived::baz, 3.14); // Visual Studio 2013 will accept this, GCC 4.8.1 won't
}
I can't get either GCC4.8.1 or VS2013 to compile both lines above. They will compile only one but not the other (and they don't agree on which line is correct and incorrect either). The error message states template deduction failed by both compilers. So what's actually wrong? I've put all the template parameters in the last line (which I thought would be deducible), but it still cannot be deduced by GCC, though VS can. Yet VS cannot deduce the template arguments for the b.foo(&Base::bar);
line when I place the template arguments for that, yet GCC can deduce them without any template arguments. Totally bewildered here. Are both compilers bugged here? Any fix possible on the programmer's part?