Template instantiation failure: compiler choosing improper overload function

Viewed 140

I'm not new to templates but I ran into a rather curious problem where I need to separate a template type into it's components for the data serializer I'm working on. It's hard to explain so I've demonstrated it.

Here's my simplified example problem, example.cpp.

template<typename T> void foo(T& arg) { }
template<typename T, typename V> void foo(T<V>& arg) { }

int main(int argc, char *argv[])
{
  foo(argc);
  return 0;
}

I get an error and then a warning which seems to indicate it's trying to instantiate both functions when only one of them is suitable.

$ g++ -Wall -W example.cpp 
example.cpp:2:43: error: ‘T’ is not a template
 template<typename T, typename V> void foo(T<V>& arg) { }
                                           ^
example.cpp: In instantiation of ‘void foo(T&) [with T = int]’:
example.cpp:6:11:   required from here
example.cpp:1:34: warning: unused parameter ‘arg’ [-Wunused-parameter]
 template<typename T> void foo(T& arg) { }
                                  ^~~

Any suggestions on how to resolve my problem and/or prevent this confusion?

1 Answers
Related