嵌套模板类型的用户定义推论指南 [英] User defined-deduction guide for nested template-types
问题描述
在这里的示例中,GCC和Clang不会产生相同的行为:
In the example here, GCC and Clang does not produce the same behavior :
#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 &&){}
};
}
简而言之,我们有一个模板参数类型,一个嵌套类型本身就是模板参数,并且模板参数之间没有任何关系.
In short, we have a template-parametered type, with a nested type which is itself template-parametered, and template parameters have no relationship between each others.
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
[...]推导指南应在与相应类模板相同的范围内声明,并且对于成员类模板,应具有相同的访问权限.[...]
[...] 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. [...]
Q2 :如果对Q1的回答否,当命名空间类型和命名空间类型之间没有关系时,为嵌套类型创建用户定义的推导指南的语法是什么?嵌套类型的模板参数?
我希望语法接近:
Q2 : If no to Q1, what is the syntax to create a user-defined deduction guide for nested type, when there is no relationship between namespace-type and nested-type template-parameters ?
I'd expect a syntax close to :
template <template <typename...> typename T, typename ... Ts>
template <typename U>
some_type<T<Ts...>>::nested<U>::nested(U&&) -> nested<U>;
但是,嵌套< U>
是错误,因为它需要推导的类型...来推导它.
However, nested<U>
is wrong, as it required a deduced type ... to deduce it.
此外,这被解释为尾随返回类型为 void
的函数.
Also, this is interpreted as a function with trailing return type void
.
template <template <typename...> typename T, typename ... Ts>
template <typename U>
typename some_type<T<Ts...>>::template nested<U>::nested(U&&) -> nested<U>;
谢谢您的时间.
推荐答案
快速修复
我发现的唯一解决方法是仅为Clang添加用户定义的推导指南
,
就可维护性而言,这是次优的方式.
godbolt上的实时示例,
或查看下面的资源.
- GCC 不允许在非命名空间上下文中使用推导, Clang 允许. 在这种情况下,
- requires语需要一个演绎指南, GCC 不需要
- GCC does not allow deduction guide in non-namespace context, Clang does.
- Clang requires a deduction guide in this context, GCC does not
NB:张贴此消息时,clang干线是11.0.1,gcc干线是10.2
#include <tuple>
template <typename T>
struct type
{
template <typename U>
struct nested
{
template <typename ... nested_Ts>
nested(U &&, std::tuple<nested_Ts...> &&)
{}
};
#if __clang__
// here, user-defined deduction guide only for Clang
template <typename U, typename ... Ts>
nested(U&&, std::tuple<Ts...>&&) -> nested<U>;
#endif
};
void instanciate_symbols()
{
using type = type<int>;
[[maybe_unused]] auto value = type::nested{'a', std::tuple{42, .42f}};
}
这篇关于嵌套模板类型的用户定义推论指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!