相关疑难解决方法(0)

如何为嵌套模板类提供演绎指南?

根据[ temp.deduct.guide/3 ]:

(...)演绎指南应在与相应的类模板相同的范围内声明,对于成员类模板,具有相同的访问权限.(......)

但是下面的例子似乎没有在[gcc][clang]中编译.

#include <string>

template <class>
struct Foo {
    template <class T>
    struct Bar {
        Bar(T) { }
    };
    Bar(char const*) -> Bar<std::string>;
};

int main() {
    Foo<int>::Bar bar("abc");
    static_cast<void>(bar);
}
Run Code Online (Sandbox Code Playgroud)

嵌套模板类的演绎指南的正确语法是什么?或者这个可能是正确的但是编译器还没有支持它?


类似的语法但没有嵌套类在gcc和clang中编译都很好:

#include <string>

template <class T>
struct Bar {
    Bar(T) { }
};
Bar(char const*) -> Bar<std::string>;

int main() {
    Bar bar("abc");
    static_cast<void>(bar);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-argument-deduction c++17

16
推荐指数
1
解决办法
537
查看次数

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
查看次数