c++ - 在模板别名中解包参数包

标签 c++ c++11 variadic-templates gcc4.9 template-aliases

我在将可变参数模板解压缩为模板别名时遇到了问题。

以下代码适用于 Clang 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 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.

最佳答案

在 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 ) 中进行的讨论 - 现在在上面的问题中进行总结。

关于c++ - 在模板别名中解包参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433658/

相关文章:

c++ - 将 decltype 与成员函数指针一起使用

c++ - 如何修复错误 "other computer the program can' t start because libgcc_s_dw2-1.dll is missing from your computer“在其他计算机上

c++ - 如何使迭代器成为 "automatically dereferenced"?

c++ - 为什么指向非常量成员函数的指针不能指向与常量成员函数相反的指针?

C++11 `nullptr_t` 返回函数被省略?

variadic-templates - 在 D 中,如何在整个元组上指定可变参数模板约束?

c++11:从模板函数构建 std::tuple

c++ - C++ 是否允许在可变参数模板参数之后使用普通参数?

c++ - 这个 C++ 程序出了什么问题?

c++ - 在 Arm 板上编译的 makefile 编译错误