小编czs*_*108的帖子

C++17 Deduction Guide 定义在类内,在类内无效,在类外有用

#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 …
Run Code Online (Sandbox Code Playgroud)

c++ template-argument-deduction c++17 deduction-guide

5
推荐指数
0
解决办法
130
查看次数