How to provide deduction guide for nested template class?

让人想犯罪 __ 提交于 2019-12-09 14:17:41

问题


According to [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?


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);
}

回答1:


[temp.deduct.guide] includes the sentence:

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.

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).

This is gcc bug 79501 (filed by Richard Smith).



来源:https://stackoverflow.com/questions/46103102/how-to-provide-deduction-guide-for-nested-template-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!