本文介绍了我如何使std :: vector类成为Sequence,以便可以将其传递给boost :: hana :: group?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我希望以下代码能够编译和工作:

I want the following code to compile and work:

#include <boost/hana/group.hpp>
#include <functional>
#include <vector>
int main() {
    std::vector<int> x = {1,1,3,4};
    auto groups = boost::hana::group(x, std::equal_to<>{});
}

尝试编译如下错误:

$ g++ -std=c++2a deleteme.cpp && ./a.out
In file included from deleteme.cpp:1:
/usr/include/boost/hana/group.hpp: In instantiation of ‘constexpr auto boost::hana::group_t::operator()(Xs&&, Predicate&&) const [with Xs = std::vector<int>&; Predicate = std::equal_to<void>]’:
deleteme.cpp:6:44:   required from here
/usr/include/boost/hana/group.hpp:55:42: error: static assertion failed: hana::group(xs, predicate) requires 'xs' to be a Sequence
   55 |         static_assert(hana::Sequence<S>::value,
      |                                          ^~~~~
/usr/include/boost/hana/group.hpp:59:28: error: use of deleted function ‘static constexpr auto boost::hana::deleted_implementation::apply(T&& ...) [with T = {std::vector<int, std::allocator<int> >&, std::equal_to<void>}]’
   59 |         return Group::apply(static_cast<Xs&&>(xs),
      |                ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
   60 |                             static_cast<Predicate&&>(pred));
      |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/boost/hana/core/dispatch.hpp:14,
                 from /usr/include/boost/hana/drop_front.hpp:18,
                 from /usr/include/boost/hana/concept/iterable.hpp:20,
                 from /usr/include/boost/hana/at.hpp:16,
                 from /usr/include/boost/hana/group.hpp:15,
                 from deleteme.cpp:1:
/usr/include/boost/hana/detail/dispatch_if.hpp:21:31: note: declared here
   21 |         static constexpr auto apply(T&& ...) = delete;
      |                               ^~~~~

因此,我理解原因是 std :: vector 不满足 Sequence 的概念,但是我应如何执行呢?

Therefore I understand that the reason is that std::vector does not satisfy the concept of Sequence, but how do I enforce that it does?

我一直在查看/usr/include/boost/hana/fwd/concept/sequence.hpp /usr/include/boost/hana/concept/sequence.hpp ,但就目前而言,这些文件中的模板元元编程在我没有任何帮助的情况下仍然有些繁重.

I've been giving a look at /usr/include/boost/hana/fwd/concept/sequence.hpp and /usr/include/boost/hana/concept/sequence.hpp, but for now the template meta-metaprogramming in those files is still a bit to heavy for me to understand it without any help.

这是Hana文档的摘录,我认为这是针对问题.关键是我不知道如何将这些处方转换为代码:

This is an excerpt from Hana's documentation, which I think addresses the question. The point is that I don't how I can translate that prescriptions to code:

推荐答案

虽然可以在Boost.Hana中实现某些概念,但它们的类型涉及仅在运行时才知道的不同维度,Boost.Hana.Sequence和作为其最小完整定义"一部分的一些概念包括:需要在编译时就知道长度和元素.

While it is possible to implement some of the concepts in Boost.Hana with types that involve different dimensions known only at run-time, Boost.Hana.Sequence and some of the concepts that are a part of its "Minimal Complete Definition" require length and elements to be known at compile-time.

  • 可折叠要求在编译时知道该长度.
  • Iterable 要求在编译时可以访问每个元素.
  • 序列需要 Foldable Iterable .
  • Foldable requires that the length be known at compile-time.
  • Iterable requires that each element be accessible at compile-time.
  • Sequence requires Foldable and Iterable.

需要 Sequence group 函数以及 std :: vector 的运行时长度使这不可能.

The group function requiring a Sequence and the run-time length of std::vector make this impossible.

计算象限 详细阐述了需要编译时与运行时信息的算法.

The section Computational Quadrants in the Boost.Hana manual elaborates on algorithms requiring compile-time vs run-time information.

( Group 概念可能是可行的,但我不认为这是问题的一部分.)

(The Group concept is likely doable, but I don't think that is part of the question.)

关于有关将第三方模板专门用于标准类型的评论,这是可能的,但被认为是错误的形式.解决方案是制作某种包装类型或提供您自己的概念.

Regarding the comments about specializing third party templates for standard types, it is possible but considered bad form. The solution is to make a wrapper type of some kind or provide your own concepts.

一条评论提出了一个将 std :: array 实现为 Sequence 的示例,因为它的长度在编译时是已知的.虽然在法律"中找不到银弹禁止同类型列表的 Sequence ,我可以说 Sequence 的前提是处理异构类型的数据结构.元素可以包含运行时数据,但是算法中的所有谓词都完全依赖于编译时".信息,因此 group 之类的功能对于 std :: array 来说将完全无用.(在前面的链接中对此进行了解释)

A comment suggested an example of implementing std::array as a Sequence since its length is known at compile-time. While I can't find a silver bullet in the "Laws" of Sequence that forbid homogeneously typed lists, I can say that the premise of Sequence is working with heterogeneously typed data structures. Elements may contain run-time data, but all of the predicates in the algorithms rely exclusively on "compile-time" information so functions like group would be completely useless for std::array. (This is explained in the aforementioned link)

这篇关于我如何使std :: vector类成为Sequence,以便可以将其传递给boost :: hana :: group?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:44