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

Error when passing template template parameter with dependent nested parameters types

本文关键字:参数 出错 类型 依赖 嵌套      更新时间:2023-10-16

为什么这种构造不起作用?

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, "???");

ADDED:此外,编译器无法推断等专业化中的类型

template<class T, template<T> class C, T X>
struct A<C<X>> { ... };

这样的技巧是标准所禁止的,还是仅仅是编译器的限制?

我怀疑这是允许的,这只是编译器搞砸了。当我使用它来获得一个变通方法时,我遇到了很多内部编译器错误;这通常是它没有被故意拒绝的迹象,加上错误消息毫无意义。

在c++20中,我可以生成这种变通方法。

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...精确匹配,只需要与它们兼容即可。

活生生的例子。