嵌套模板类型的用户定义推导指南
Q1:命名空间范围内是否允许用户定义的推导指南?
在这里的示例中,GCC 和 Clang 不会产生相同的行为:
- https://godbolt.org/z/8W6hznEjo
#include <tuple>
template <typename T>
struct some_type;
template <template <typename...> typename T, typename ... Ts>
struct some_type<T<Ts...>>
{
template <typename U>
class nested
{
U member;
public:
nested(U &&){}
};
// non-namespace scope user-deduction-guide : OK with Clang, fix the deduction issue
template <typename U>
nested(U&&) -> nested<U>;
};
void func()
{
using pack_type = std::tuple<int, char>;
some_type<pack_type>::nested{
[](auto &&){}
};
}
简而言之,我们有一个模板参数类型,嵌套类型本身就是模板参数,模板参数之间没有关系。
template <typename T>
struct some_type;
template <template <typename...> typename T, typename ... Ts>
struct some_type<T<Ts...>>
{
template <typename U>
class nested // <- nested type, where `U` as no relationship with `T<Ts...>`
{
U member;
public:
nested(U &&);
};
};
标准指定:http : //eel.is/c++draft/temp.deduct.guide#3
[...] 应在与相应类模板相同的范围内声明演绎指南,并且对于成员类模板,应具有相同的访问权限。[...]
Q2 : 如果没有Q1,命名空间类型和嵌套类型模板参数之间没有关系时,为嵌套类型创建用户定义推导指南的语法是什么?
我希望语法接近:
template <template <typename...> typename T, typename ... Ts>
template <typename U>
some_type<T<Ts...>>::nested<U>::nested(U&&) -> nested<U>;
然而,nested<U>是错误的,因为它需要一个推导类型......来推导它。
此外,这被解释为具有尾随返回类型的函数void。
template <template <typename...> typename T, typename ... Ts>
template <typename U>
typename some_type<T<Ts...>>::template nested<U>::nested(U&&) -> nested<U>;
谢谢你的时间。