打开模板别名中的参数包装 [英] Unpacking paramater packs in template aliases

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

问题描述



以下代码适用于Clang 3.4和GCC 4.8,但是与GCC 4.9失败:

p>

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

template< typename ... ts>
struct foo
{
使用front = front_type< Ts ...> ;;
};

GCC 4.9抱怨:

  test.cc:7:37:error:别名模板的非包装参数'T'的包扩展参数模板< class T,class ...> use front_type = T'
using front = front_type< Ts ...>
^
test.cc:1:15:note:declaration here
template< typename T,typename ...>
^

存在一个已提交的GCC错误(#59498 ),但是这应该是失败?以下是 C ++核心语言问题#1430的一些上下文扩展到固定别名模板参数列表


最初,包扩展无法扩展为固定长度的模板参数列表,但在N2555中已更改。



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

 模板< class T,class U,class V> 
struct S {};

template< class T,class V>
使用A = S ;

template< class ... Ts>
void foo(A< Ts ...>);

没有办法表达 A< Ts ...& / code>在 S ,所以我们需要持有 A



目前,EDG和Clang拒绝此操作testcase,抱怨太少的模板参数为A. G ++也做了,但我认为这是一个错误。但是,在ABI列表上,John Spicer认为它应该被拒绝。



解决方案

在gcc4.9 bugzilla中报告:



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



要重现的最小代码

 模板< typename T,typename ...> 
using alias = T;

template< typename ... T>
使用variadic_alias = alias< T ...> ;;

使用Fail = variadic_alias< int> ;;

int main(){}





$ b b

从gcc的解释 - 这不是那么明显这是一个真正的bug。
这仍然是讨论在gcc bugzilla和在DR 1430( http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1430 ) - 上述问题现在的摘要。


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

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 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...>
               ^

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":

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

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.

解决方案

It was reported in gcc4.9 bugzilla:

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

Minimal code to reproduce

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

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

using Fail = variadic_alias<int>;

int main() { }


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天全站免登陆