Search code examples
c++templatesg++c++17template-argument-deduction

Class template argument deduction failed with derived class


#include <utility>

template<class T1, class T2>
struct mypair : std::pair<T1, T2>
{ using std::pair<T1, T2>::pair; };

int main()
{
    (void)std::pair(2, 3); // It works
    (void)mypair(2, 3);    // It doesn't work
}

Is the above well formed?

Is it possible deduce the class template arguments in the second case if the constructors are being inherited? Are the constructors of std::pair participating in the creation of implicit deduction guides for mypair?

My compiler is g++ 7.2.0.


Solution

  • See Richard Smith's answer.


    A previous version of this answer had stated that the following should work

    template <class T> struct B { B(T ) { } };
    template <class T> struct D : B<T> { using B<T>::B; };
    
    B b = 4; // okay, obviously
    D d = 4; // expected: okay
    

    But this isn't really viable, and wouldn't even be a good idea to work as I thought it would (we inherit the constructors but not the deduction guides?)