如何解决如何将std :: vector类设为Sequence,以便可以将其传递给boost :: hana :: group?
我希望以下代码能够编译和工作:
#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
的概念,但是我如何强制这样做呢?
我一直在研究/usr/include/boost/hana/fwd/concept/sequence.hpp
和/usr/include/boost/hana/concept/sequence.hpp
,但就目前而言,这些文件中的模板元元编程对于我而言毫无帮助地理解它仍然很繁重。 / p>
This is an excerpt from Hana's documentation,我认为可以解决这个问题。关键是我不知道如何将处方转换为代码:
因此,有必要专门 Hana命名空间中的Sequence元函数告诉Hana一种类型 确实是一个序列。明确专精 序列功能可以看成是封条,上面写着“此数据 类型满足
Sequence
“的其他定律,因为那些定律不能 由Hana自动检查。
解决方法
虽然可以在Boost.Hana中实现某些概念,这些类型涉及仅在运行时才知道的涉及不同维度的类型,但Boost.Hana.Sequence及其某些概念是其“最小完整定义”的一部分”,要求长度和元素在编译时是已知的。
-
Foldable
要求在编译时知道该长度。 -
Iterable
要求在编译时可以访问每个元素。 -
Sequence
需要Foldable
和Iterable
。
需要group
的{{1}}函数和运行时长Sequence
使得这不可能。
Boost.Hana手册中的Computational Quadrants部分详细介绍了需要编译时与运行时信息的算法。
(std::vector
概念可能是可行的,但我不认为这是问题的一部分。)
关于专门针对标准类型的第三方模板的评论,可能但被认为是错误的形式。解决方案是制作某种包装类型或提供您自己的概念。
编辑:
一条评论提出了一个将Group
实现为std::array
的示例,因为它的长度在编译时是已知的。虽然我在Sequence
的“法律”中找不到禁止同类型的列表的灵丹妙药,但我可以说Sequence
的前提是处理异构类型的数据结构。元素可能包含运行时数据,但是算法中的所有谓词都完全依赖于“编译时”信息,因此Sequence
之类的功能对于group
完全没有用。 (在前面的链接中对此进行了解释)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。