0

I'm trying to write a variadic template function that calls single-argument overloads as base cases. The base cases cannot be declared before my variadic because they involve types defined elsewhere. I need this to work with gcc-4.7.2. I've got the following MWE.

template<class T, class U, class... Args>
void print(T const& t, U const& u, Args const&... args)
{
    print(t);
    print(u, args...);
}

void print(int){}
void print(float){}

int main(int, char*[])
{
    int i;
    float f;
    print(i, f);
}

My original worked with VC, but this MWE fails in gcc-4.7.2 with the following.

main.cpp: In instantiation of ‘void print(const T&, const U&, const Args& ...) [with T = int; U = float; Args = {}]’:
main.cpp:15:12:   required from here
main.cpp:4:2: error: no matching function for call to ‘print(const int&)’
main.cpp:4:2: note: candidate is:
main.cpp:2:6: note: template<class T, class U, class ... Args> void print(const T&, const U&, const Args& ...)
main.cpp:2:6: note:   template argument deduction/substitution failed:
main.cpp:4:2: note:   candidate expects 3 arguments, 1 provided
main.cpp:5:2: error: no matching function for call to ‘print(const float&)’
main.cpp:5:2: note: candidate is:
main.cpp:2:6: note: template<class T, class U, class ... Args> void print(const T&, const U&, const Args& ...)
main.cpp:2:6: note:   template argument deduction/substitution failed:
main.cpp:5:2: note:   candidate expects 3 arguments, 1 provided

Shouldn't second stage lookup be kicking in and finding the overloads declared below?

John
  • 7,301
  • 2
  • 16
  • 23

1 Answers1

1

void print(int);, void print(float); should be declared before.
print(MyClass) can be declared after thanks to ADL.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Yes. In my real code I had to move the `string` and `vector` overloads into the framework rather than leaving them to the client to define. – John Jan 02 '18 at 19:08