我在 Debian 挤压上使用 gcc 4.4。考虑以下代码。
#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;
}
int main(void)
{
map<string, int> a = foo<string, map, int>();
}
所以,这里的想法是T
matches string
、C
matchesmap
和模板参数包Args
matches int
。我可能有一些语法错误,如果是,请更正。特别是,如果一个人希望第一个模板参数class C
匹配T
,其余的匹配模板参数包Args
,template <typename T, typename... Args> class C
那么语法是否正确?
这给出了错误
In function 'int main()':
post.cc:18: error: no matching function for call to 'foo()'
这似乎类似于问题Variadic template templates and perfect forwarding。这个问题表明这是一个 gcc 错误,但也许我错误地认为这些问题是关于同一件事的。
请温柔一点。我对可变参数模板的了解还不到 12 小时;我只是想重写一些旧的 C++ 代码以减少重复。我也有一段时间没有做任何 C++ 了。如果有解决方法,请告诉我。谢谢。
编辑:Variadic 模板模板的评论中建议的解决方法和Ise Wisteria的完美转发对我有用,这表明这是同一个错误。当然,我现在(a)想知道这种解决方法有多脆弱,(b)它为什么有效,以及是什么促使 Ise 想到它。虽然我猜只有一诚可以回答最后一点。:-)