5
#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.

czs108
  • 151
  • 4
  • 1
    Accepted by clang, probably a bug in msvc. – n. m. could be an AI Sep 30 '20 at 06:05
  • Cannot reproduce: https://godbolt.org/z/51hTG6 – parktomatomi Oct 14 '20 at 11:59
  • I get compile errors with `g++ -std=c++17`: deduction guide ‘Test::overloaded(Ts) -> Test::overloaded’ must be declared at namespace scope – Elliott Oct 14 '20 at 12:12
  • This question seems very related to: https://stackoverflow.com/questions/50600645/deduction-guides-templates-and-subobjects-which-compiler-is-right – Elliott Oct 14 '20 at 12:15
  • From the answer it seems that it's a bug in both `gcc` and `msvc`. Given @parktomatomi's comment, perhaps you could provide some more details about your Visual Studio version? x86 or x64? and the exact number of the version? – Elliott Oct 14 '20 at 12:21
  • @Elliott Visual Studio 2019 version 16.8 Preview 3.2 or 4.0; x86; The C++ standard is `C++17`. – czs108 Oct 15 '20 at 02:27

0 Answers0