Search code examples
c++visual-c++variadic-templatesusing

template parameter pack in using


How i can use "using" keyword in multiple inheritance when parameter pack is template parameter of base class?

Code below compiles just fine

struct A
{
    void foo(int) {}
};

struct B
{
    void foo(int) {}
};

template<typename ...Types>
struct C : public Types...
{
   using Types::foo...;
};

int main()
{
    C<A,B> c;
}

But if i use template instead of A and B - I've got compile error

template<typename T> 
struct TA {};

template<>
struct TA<int>
{ 
    void foo(int) {} 
};

template<>
struct TA<double>
{
    void foo(int) {}
};

template<typename ...Types>
struct TC : public TA<Types>...
{
    using TA<Types>::foo...; // ERROR C3520
};

Error:

error C3520: 'Types': parameter pack must be expanded in this context

How to rewrite second piece of code to get it work?

PS I tried this code with gcc and it's compiles without errors. But now I am using msvc...


Solution

  • Since it's a known MSVC bug, if you still want to make this work with MSVC you'll have to do it the (not very) hard way:

    template <typename ...Types>
    struct TC;
    
    template <typename T>
    struct TC<T> : TA<T>
    {
        using TA<T>::foo;
    };
    
    template <typename T, typename ...Types>
    struct TC<T, Types...> : public TC<T>, public TC<Types...>
    {
        using TC<T>::foo;
        using TC<Types...>::foo;
    };