如何为嵌套模板类提供推导? [英] How to provide deduction guide for nested template class?
问题描述
根据[ 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. (...)
But below example doesn't seem to compile in both [gcc] and [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);
}
嵌套模板类的推导指南的正确语法是什么?也许这是正确的,但是编译器尚不支持?
What is the correct syntax of deduction guide for nested template class? Or maybe this one is correct but it isn't yet supported by the compilers?
类似的语法,但没有嵌套类,在gcc中都可以正常编译和clang:
Similar syntax but without nested class compiles fine both in gcc and 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
-检查并检查)。
This suggests that your example should work - deduction guides are explicitly supported for member class templates, as long as they're declared in the same scope and access (which would be the class scope and public
- check and check).
这是 gcc错误79501 (由Richard Smith提出)。
This is gcc bug 79501 (filed by Richard Smith).
这篇关于如何为嵌套模板类提供推导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!