gpt4 book ai didi

c++ - 传递具有依赖嵌套参数类型的模板模板参数时出错

转载 作者:行者123 更新时间:2023-12-04 12:04:01 25 4
gpt4 key购买 nike

为什么这个结构不起作用?

Visual Studio 显示错误 C3201:类模板“AA”的模板参数列表与模板参数“C”的模板参数列表不匹配。但是好像是<int, char, bool>在这两种情况下。

template<int I, char C, bool B>
struct AA
{
static const int i = I;
static const char c = C;
static const bool b = B;
};

template<typename... T>
struct outer
{
template <template<T... > typename C>
struct inner
{
template<T... X>
using type = C<X...>;
};
};

static_assert(outer<int, char, bool>::inner<AA>::type<5, 'a', true>::i == 5, "???");

添加:此外,编译器无法推断像这样的特化类型
template<class T, template<T> class C, T X>
struct A<C<X>> { ... };

标准是否禁止此类技巧,还是仅仅是编译器限制?

最佳答案

我怀疑这是允许的,这只是编译器搞砸了。当我尝试使用它来获得解决方法时,我遇到了很多内部编译器错误;这通常表明它没有被故意拒绝,而且错误消息毫无意义。
我可以生成此解决方法。

template<int I, char C, bool B>
struct AA
{
static const int i = I;
static const char c = C;
static const bool b = B;
};

template<template<auto... > typename C, typename... Ts>
struct outer_base
{
struct inner
{
template<Ts... X>
using type = C<X...>;
};
};


template<typename... Ts>
struct outer
{
template <template<auto... > typename C>
using inner = typename outer_base<C, Ts...>::inner;
};

static_assert(outer<int, char, bool>::inner<AA>::type<5, 'a', true>::i == 5, "???");
这比您想要的要少一些,因为它不需要 C匹配类型 Ts...确切地说,只是与它们兼容。
Live example .

关于c++ - 传递具有依赖嵌套参数类型的模板模板参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250921/

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