Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when passing template template parameter with dependent nested parameters types

Why this construction doesn't work?

Visual Studio shows error C3201: the template parameter list for class template 'AA' does not match the template parameter list for template parameter 'C'. But it seems to be <int, char, bool> in both cases.

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: Moreover, compiler can't infer types in specializations like

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

Are such tricks prohibited by standard or is it just compiler limitations?

like image 832
aaalex88 Avatar asked Feb 15 '17 13:02

aaalex88


1 Answers

I suspect this is allowed, and this is just the compiler screwing up. When I played with it to get a workaround working, I got a lot of internal compiler errors; that is usually a sign of it not being rejected on purpose, plus the error message makes no sense.

In c++20 I can produce this workaround.

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

which is a bit less constrained than you might like, as it doesn't require C to match the types Ts... exactly, just be compatible with them.

Live example.

like image 105
Yakk - Adam Nevraumont Avatar answered Oct 21 '22 04:10

Yakk - Adam Nevraumont