C++17 Deduction Guide defined inside a class is not effective inside this class, but useful outside the class

396 views Asked by At
#include <variant>

struct A { };

struct B { };

struct Test
{
    template<typename Ts>
    struct overloaded : Ts { };

    // 1st Deduction Guide
    template<typename Ts>
    overloaded(Ts)->overloaded<Ts>;

    // 2nd Deduction Guide for class "A"
    overloaded(A)->overloaded<A>;

    void Fun()
    {
        // OK
        overloaded obj1{ A{} };

        // Error: No instance of constructor matches the argument list
        // I think the 1st Deduction Guide is not effective inside class "Test"
        overloaded obj2{ B{} };
    }
};

int main()
{
    // All Deduction Guides in class "Test" are effective here

    // OK
    Test::overloaded obj1{ A{} };

    // OK
    Test::overloaded obj2{ B{} };

    return 0;
}

I defined some C++17 Deduction Guides inside a class called Test.

But I found that the 1st Deduction Guide, which includes a template argument Ts is not effective inside class Test itself. But effective outside it, such as in main function.

template<typename Ts>
overloaded(Ts)->overloaded<Ts>;

The 2nd Deduction Guide which does not include a template argument is effective both inside and outside the class.

overloaded(A)->overloaded<A>;

Can anyone tell me why?

My IDE is Visual Studio 2019, the C++ standard is C++17.

Thank you.

0

There are 0 answers