Search code examples
c++templatesinheritancevisual-studio-2017using

Inherit base constructor with base class being a template parameter (MSVC)


I try to achieve this code using VS2017, and get errors :

template <class T>
class A {
    public :
    A() {}
};

template < template <class U> class T, class U>
class B : public T<U> {
    using T<U>::T;//Errors : # 'T': is not a member of 'A<U>' # 'T': symbol cannot be used in a using-declaration
};

int main() {
    B<A, int> test;
    return 0;
}

Works perfectly using Clang and GCC according to https://wandbox.org/

I'd like to know why it's not working on Visual Studio, and how to fix it. Looks like VS doesn't want to consider second 'T' parameter as a template.

Here is another question previously asked the closest I could find about this matter. Couldn't find a solution reading it though : Inheriting constructors from a template base class


Solution

  • The workaround is to define a typedef alias

    typedef T<U> base_t;
    

    and use it to inherit the constructors from the base class:

    using base_t::base_t;
    

    This is not a unique compiler bug; I dimly recall a similar issue with an old version of gcc.