gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:00:58 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com