根据 [ 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/