在模板别名中解压参数包 [英] Unpacking parameter packs in template aliases

查看:128
本文介绍了在模板别名中解压参数包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了将可变参数模板打包成模板别名的问题.

I run into a problem with unpacking variadic templates into a template alias.

以下代码适用于Clang 3.4和GCC 4.8,但不适用于GCC 4.9:

The following code works with Clang 3.4 and GCC 4.8 but fails with GCC 4.9:

template <typename T, typename...>
using front_type = T;

template <typename... Ts>
struct foo
{
  using front = front_type<Ts...>;
};

GCC 4.9抱怨:

GCC 4.9 complains:

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 ++核心语言第1430期"pack扩展到固定别名模板参数列表" :

There exists a filed GCC bug (#59498), but is this supposed to fail? Here is some context from the C++ core language issue #1430, "pack expansion into fixed alias template parameter list":

最初,包扩展无法扩展为固定长度的模板参数列表,但N2555中对此进行了更改.这对于大多数模板都可以正常工作,但是会导致别名模板出现问题.

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.

在大多数情况下,别名模板是透明的.当在模板中使用它时,我们可以仅替换依赖模板的参数.但是,如果template-id对非可变参数使用pack扩展,则此方法不起作用.例如:

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...>);

无法用S来表示A<Ts...>,因此我们需要按住A直到有T可以替代,因此需要进行整形处理

There is no way to express A<Ts...> in terms of S, so we need to hold onto the A until we have the Ts to substitute in, and therefore it needs to be handled in mangling.

当前,EDG和Clang拒绝了这个测试用例,抱怨A的模板参数太少.G++也这样做,但是我认为那是一个错误.但是,在ABI名单上,约翰·斯派塞(John Spicer)认为应予以拒绝.

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.

推荐答案

它是在gcc4.9 bugzilla中报告的:

It was reported in gcc4.9 bugzilla:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59498

可复制的最小代码

template <typename T, typename ...>
using alias = T;

template <typename ...T>
using variadic_alias = alias<T...>;

using Fail = variadic_alias<int>;

int main() { }


根据海湾合作委员会(GCC)人士的解释-并不是很明显,这是一个真正的错误. 仍在gcc bugzilla和DR 1430( http://open-std .org/jtc1/sc22/wg21/docs/cwg_active.html#1430 )-上面问题中的摘要.


From the explanation from gcc folks - it is not so obvious this is a real bug. This is still discussion held in gcc bugzilla and in DR 1430 (http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1430) - summary in now in the question above.

这篇关于在模板别名中解压参数包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆