aaa*_*x88 6 c++ templates template-templates variadic-templates
为什么这种结构不起作用?
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, "???");
Run Code Online (Sandbox Code Playgroud)
ADDED:此外,编译器无法推断出专业化类型
template<class T, template<T> class C, T X>
struct A<C<X>> { ... };
Run Code Online (Sandbox Code Playgroud)
这些技巧是否被标准禁止或只是编译器限制?
我怀疑这是允许的,这只是编译器搞砸了。当我使用它来解决问题时,我遇到了很多内部编译器错误;这通常表明它没有被故意拒绝,而且错误消息毫无意义。
在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, "???");
Run Code Online (Sandbox Code Playgroud)
这比您想要的限制要少一些,因为它不需要完全C匹配类型Ts...,只需与它们兼容即可。