I\'m using gcc 4.4 on Debian squeeze. Consider the following code.
#include
As discussed in the edits, my question appears to tickle the same bug as the linked question, Variadic template templates and perfect forwarding. In particular, the workaround given there in a link also works in my case. The modified code that works is as follows:
#include <map>
#include <string>
using std::map;
using std::string;
template <typename T,
template <typename T, typename... Args> class C,
typename... Args>
struct X
{
typedef C<T, Args...> type;
};
template <typename T,
template <typename T, typename... Args> class C,
typename... Args>
typename X<T, C, Args...>::type foo()
{
C<T, Args...> x;
return x;
}
int main(void)
{
map<string, int> a = foo<string, map, int>();
}
I don't think variadic template parameters can match non-variadic arguments in g++4.4, so you need to overload your foo function with a non-variadic version.
Also keep in mind that map actually has more than two template parameters, and therefor wont match the new foo-function either.
This addition to your example should clarify it:
#include <map>
#include <string>
using std::map;
using std::string;
// Args lets the user specify additional explicit template arguments
template <typename T,
template <typename T, typename... Args> class C,
typename... Args>
C<T, Args...> foo() {
C<T, Args...> x;
return x;
}
template<typename T, template<typename, typename> class C, typename Arg>
C<T, Arg> foo() {
return C<T, Arg>();
}
template<typename T, typename... Args> class A {};
template<typename T, typename Arg> class B {};
int main(void) {
map<string, int> a = foo<string, map, int>(); // fails.
A<string, int> x = foo<string, A, int>();
B<string, int> y = foo<string, B, int>();
}