c++ - 如何为嵌套模板类提供推演指南?

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

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

(...) A deduction-guide shall be declared in the same scope as the corresponding class template and, for a member class template, with the same access. (...)

但下面的示例似乎无法在 [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);
}

嵌套模板类的推导指南的正确语法是什么?或者也许这个是正确的,但编译器还不支持?


类似的语法,但没有嵌套类,在 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);
}

最佳答案

[temp.deduct.guide]包括句子:

A deduction-guide shall be declared in the same scope as the corresponding class template and, for a member class template, with the same access.

这表明您的示例应该可以工作 - 成员类模板明确支持演绎指南,只要它们在相同的范围和访问权限中声明(即类范围和 public - 检查和检查)。

这是gcc bug 79501 (由理查德·史密斯归档)。

关于c++ - 如何为嵌套模板类提供推演指南?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46103102/

相关文章:

c++ - 寻找滥用枚举的替代方法

python - 在文件中存储带有函数调用的 f 字符串

html - Outlook 2010 与 Outlook 2013 的电子邮件 html 模板

c++ - 为什么 std::iota 不是 constexpr?

c++ - Opencv C++ : How to split video several part by time?

c++ - 对纯右值的左值引用的地址代表什么?

c++ - 如何使 C++ lambda 对象的存储更高效?

python - 如何在模板中迭代多维列表/字典

c++ - 通用初始化 - vector 的填充构造函数

c++ - Const 类型转换空基类