问题描述
考虑这个最小的例子:
template <typename T, typename U> struct foo {}; template <template <typename...> class Bar> struct converter { template <typename... Args> converter(const Bar<Args...> &); }; int main() { converter<foo> c(foo<int,double>()); // This works. // converter<foo> c = foo<int,double>(); This fails }
注释掉的行与GCC 4.5和4.6一个消息像:
The commented-out line fails with both GCC 4.5 and 4.6, with a message like:
main.cpp:10:2: error: wrong number of template arguments (1, should be 2) main.cpp:4:8: error: provided for template<class T, class U> struct foo main.cpp: In function int main(): main.cpp:15:37: error: conversion from foo<int, double> to non-scalar type converter<foo> requested
如果使用特定数量的模板参数而不是使用可变参数模板在这种情况下)没有错误。我有点困惑,因为我预期这两行是完全等价的:这是一个预期的行为?
If, instead of using variadic templates, the specific number of template parameters is used (i.e., 2 in this case) there are no errors. I'm a bit confused since I expected the two lines to be exactly equivalent: is this an expected behaviour?
推荐答案
这应该是工作。这是一个GCC错误。 GCC不支持C ++ 0x可变参数模板到最大(但是公平,规范仍然在不断变化的细节)。
Yes, this is supposed to work. It's a GCC error. GCC doesn't support C++0x variadic templates to the fullest yet (and to be fair, the specification is still constantly changing in details).
你说的这个工作真的声明一个函数;它不会初始化一个对象,这是你想要的。
What you say "This works" is really declaring a function; it doesn't initialize an object, which was what you intended.
对于你想要的,见14.3.3p3,它描述了如何 template< typename ...> class Bar 可以匹配 foo 和14.8.2.5p9,其描述 foo 可以匹配 foo< int,double> 。
For what you intended, see 14.3.3p3 which describes how template<typename...> class Bar can match foo, and 14.8.2.5p9 which describes how foo<Args...> can match foo<int, double>.
这篇关于可变模板和副本构造通过分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!