结构化绑定宽度 [英] Structured bindings width

查看:64
本文介绍了结构化绑定宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以确定我应该使用结构化绑定语法在方括号中指定多少个变量名,以匹配简单的右侧 struct 数据成员的数量我想成为通用库的一部分,该库使用结构化绑定将任意类分解为其组成部分。目前没有结构化绑定的可变版本(并且我认为不能针对当前的语法提出),但是我的第一个想法是对某些函数 decompose()进行重载。,它将 struct 参数分解为一组其成分。 decompose()应该重载参数数据成员(即 struct )的数量。目前, constexpr if 语法也可以用于分派此语法。但是,如何为上述目的模拟类似于 sizeof ... 运算符的东西?我不能在SFINAE构造中的某处使用 auto [a,b,c] 语法,因为它是分解声明和AFAIK,任何声明都不能可以在 decltype 内部使用,我也不能在lambda函数的主体中使用它,因为lambda函数也不能在模板参数中使用。



我当然想拥有内置运算符(语法如 sizeof [] S / sizeof [](S)用于 S类),但也可以接受以下内容:

  template< typename类型,typename = void> 
struct sizeof_struct
{

};

模板<类型名称类型>
struct sizeof_struct<类型,std :: void_t< decltype([] {auto& [p1] = std :: declval< type>(); void(p1);})> >
:std :: integral_constant< std :: size_t,1>
{

};

模板<类型名称类型>
struct sizeof_struct<类型,std :: void_t< decltype([] {auto& [p1,p2] = std :: declval< type>(); void(p1); void(p2);})> >
:std :: integral_constant< std :: size_t,2>
{

};

...等到一定程度的合理度

也许 constexpr lambda将允许我们将它们用作模板的参数。您怎么看?



出现概念是否可能?

解决方案

  struct two_elements {
int x;
y倍;
};

struct Five_elements {
std :: string one;
std :: unique_ptr< int>二;
int * 3;
字符四;
std :: array< two_elements,10>五;
};

构造任何内容{
template< class T>运算符T()const;
};

命名空间详细信息{
template< class T,class Is,class = void>
struct can_construct_with_N:std :: false_type {};

template< class T,std :: size_t ... Is>
struct can_construct_with_N< T,std :: index_sequence< Is ...> ;, std :: void_t< decltype(T {(void(Is),anything {})...})>> ;:
std :: true_type
{};
}
template< class T,std :: size_t N>
使用can_construct_with_N = details :: can_construct_with_N< std :: make_index_sequence< N> ;;

命名空间详细信息{
template< std :: size_t Min,std :: size_t范围,template< std :: size_t N>类目标>
结构最大化:
std :: conditional_t<
maximum< Min,Range / 2,target> {} ==(Min + Range / 2)-1,
maximum< Min + Range / 2,(Range + 1)/ 2,target> ,
maximum< Min,Range / 2,target>
>
{};
template< std :: size_t最小值,模板< std :: size_t N>类目标>
结构最大化< Min,1,target> ;:
std :: conditional_t<
target< Min> {},
std :: integral_constant< std :: size_t,Min> ;、
std :: integral_constant< std :: size_t,Min-1>
>
{};
template< std :: size_t最小值,模板< std :: size_t N>类目标>
结构最大化< Min,0,target> ;:
std :: integral_constant< std :: size_t,Min-1>
{};

template< class T>
struct Constructor_searcher {
template< std :: size_t N>
using result = :: can_construct_with_N< T,N> ;;
};
}

template< class T,std :: size_t Cap = 20>
使用construct_airity = details :: maximize< 0,上限,详细信息:: construct_searcher< T ::模板结果> ;;

此操作会对 T 从0到20。20是一个常数,您可以根据需要在编译时和内存上增加它。



实时示例



如果结构中的数据不能从其自身类型的右值构造出来,它在C ++ 14中不起作用,但是我相信在C ++ 17中发生过冠词省略(())



将其转换为结构化绑定需要大量的手动代码。但是一旦有了,您应该可以问诸如此结构的第3种类型是什么之类的问题。



如果在不完成 tuple_size 的情况下,可以将结构分解为结构化绑定,



不幸的是,即使在C ++中, std :: tuple_size 也不是SFINAE友好的。 17。但是,使用 tuple_size 部分的类型也需要ADL启用 std :: get



使用 failure_tag get< std :: size_t>(Ts const& ...)创建一个使用std :: get 。使用它来检测它们是否在类型(!std :: is_same< get_type< T,0>,failure_tag>上重写 get< 0> ; {} ),如果是这样,请沿着 tuple_element 路径确定是否适合。将结果元素填充到 decltype(get< Is>(x)) std :: tuple 中并返回



如果失败,请使用上面的 construct_airity ,并使用该值来了解如何在类型。为了统一起见,我可能会将其发送到 std :: tie 中。



我们现在有 tuple_it 接受类似结构绑定的任何内容,并将其转换为引用或值的元组。
现在两条路径已经融合,您的通用代码更容易了!


Is it possible to determine how many variable names should I to specify in square brackets using structured bindings syntax to match the number of data members of a plain right hand side struct?

I want to make a part of generic library, which uses structured bindings to decompose arbitrary classes into its constituents. At the moment there is no variadic version of structured bindings (and, I think, cannot be for current syntax proposed), but my first thought is to make a set of overloadings of some function decompose(), which performs decomposition of struct parameter into a set of its constituents. decompose() should be overloaded by number of parameter's (which is struct) data members. Currently constexpr if syntax also can be used to dispatch this. But how can I emulate something similar to sizeof... operator for above purposes? I can't use auto [a, b, c] syntax somewhere in SFINAE constructions, because it is a decomposition declaration and AFAIK any declaration cannot be used inside decltype, also I cannot use it for my purposes in the body of lambda functions because lambda functions cannot be used inside template arguments too.

Surely I want to have builtin operator (with syntax like sizeof[] S/sizeof[](S) for class S), but something like the following is also would be acceptable:

template< typename type, typename = void >
struct sizeof_struct
{

};

template< typename type >
struct sizeof_struct< type, std::void_t< decltype([] { auto && [p1] = std::declval< type >(); void(p1); }) > >
    : std::integral_constant< std::size_t, 1 >
{

};

template< typename type >
struct sizeof_struct< type, std::void_t< decltype([] { auto && [p1, p2] = std::declval< type >(); void(p1); void(p2);  }) > >
    : std::integral_constant< std::size_t, 2 >
{

};

... etc up to some reasonable arity

Maybe constexpr lambda will allow us to use them into template's arguments. What do you think?

Will it be possible with coming Concepts?

解决方案

struct two_elements {
  int x;
  double y;
};

struct five_elements {
  std::string one;
  std::unique_ptr<int> two;
  int * three;
  char four;
  std::array<two_elements, 10> five;
};

struct anything {
  template<class T> operator T()const;
};

namespace details {
  template<class T, class Is, class=void>
  struct can_construct_with_N:std::false_type {};

  template<class T, std::size_t...Is>
  struct can_construct_with_N<T, std::index_sequence<Is...>, std::void_t< decltype(T{(void(Is),anything{})...}) >>:
  std::true_type
  {};
}
template<class T, std::size_t N>
using can_construct_with_N=details::can_construct_with_N<T, std::make_index_sequence<N>>;

namespace details {
  template<std::size_t Min, std::size_t Range, template<std::size_t N>class target>
  struct maximize:
    std::conditional_t<
      maximize<Min, Range/2, target>{} == (Min+Range/2)-1,
      maximize<Min+Range/2, (Range+1)/2, target>,
      maximize<Min, Range/2, target>
    >
  {};
  template<std::size_t Min, template<std::size_t N>class target>
  struct maximize<Min, 1, target>:
    std::conditional_t<
      target<Min>{},
      std::integral_constant<std::size_t,Min>,
      std::integral_constant<std::size_t,Min-1>
    >
  {};
  template<std::size_t Min, template<std::size_t N>class target>
  struct maximize<Min, 0, target>:
    std::integral_constant<std::size_t,Min-1>
  {};

  template<class T>
  struct construct_searcher {
    template<std::size_t N>
    using result = ::can_construct_with_N<T, N>;
  };
}

template<class T, std::size_t Cap=20>
using construct_airity = details::maximize< 0, Cap, details::construct_searcher<T>::template result >;

This does a binary search for the longest construction airity of T from 0 to 20. 20 is a constant, you can increase it as you will, at compile-time and memory cost.

Live example.

If the data in your struct cannot be constructed from an rvalue of its own type, it won't work in C++14, but I believe guanteed elision occurs in C++17 here (!)

Turning this into structured bindings requires more than a bit of a pile of manual code. But once you have, you should be able to ask questions like "what is the 3rd type of this struct" and the like.

If a struct can be decomposed into structured bindings without the tuple_size stuff being done, the airity of it determines how many variables it needs.

Unfortunetally std::tuple_size is not SFINAE friendly even in C++17. But, types that use the tuple_size part also need to ADL-enable std::get.

Create a namespace with a failure_tag get<std::size_t>(Ts const&...) that using std::get. Use that to detect if they have overridden get<0> on the type (!std::is_same< get_type<T,0>, failure_tag >{}), and if so go down the tuple_element path to determine airity. Stuff the resulting elements into a std::tuple of decltype(get<Is>(x)) and return it.

If that fails, use the above construct_airity, and use that to figure out how to use structured bindings on the type. I'd probably then send that off into a std::tie, for uniformity.

We now have tuple_it which takes anything structured-binding-like and converts it to a tuple of references or values. Now both paths have converged, and your generic code is easier!

这篇关于结构化绑定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆