我遇到了将可变参数模板解包为模板别名的问题。打开模板别名中的参数包
以下代码可与锵3.4和GCC 4.8但失败GCC 4.9:
template <typename T, typename...>
using front_type = T;
template <typename... Ts>
struct foo
{
using front = front_type<Ts...>;
};
GCC 4.9抱怨:
test.cc:7:37: error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ...> using front_type = T'
using front = front_type<Ts...>;
^
test.cc:1:15: note: declared here
template <typename T, typename...>
^
存在一个日提交GCC蝽(#59498),但这应该失败?下面是从C++ core language issue #1430, "pack expansion into fixed alias template parameter list"一些背景:
Originally, a pack expansion could not expand into a fixed-length template parameter list, but this was changed in N2555. This works fine for most templates, but causes issues with alias templates.
In most cases, an alias template is transparent; when it's used in a template we can just substitute in the dependent template arguments. But this doesn't work if the template-id uses a pack expansion for non-variadic parameters. For example:
template<class T, class U, class V> struct S {}; template<class T, class V> using A = S<T, int, V>; template<class... Ts> void foo(A<Ts...>);
There is no way to express
A<Ts...>
in terms ofS
, so we need to hold onto theA
until we have theT
s to substitute in, and therefore it needs to be handled in mangling.Currently, EDG and Clang reject this testcase, complaining about too few template arguments for A. G++ did as well, but I thought that was a bug. However, on the ABI list John Spicer argued that it should be rejected.
你可以在'foo'之外移动'front_type' - 它们似乎没有关系吗? – PiotrNycz
是的,好点@PiotrNycz。 (虽然它没有修复它。) – mavam
我用gcc4.8检查了这个修改过的代码 - 看起来很有效。 – PiotrNycz