我可以在boost :: adaptors :: std :: array的结果上使用boost :: copy_range来返回另一个std :: array吗? [英] Can I use boost::copy_range on the result of boost::adaptors::transformed over std::array to get back another std::array?

查看:71
本文介绍了我可以在boost :: adaptors :: std :: array的结果上使用boost :: copy_range来返回另一个std :: array吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有效并生成正确的输出( 23 ,因为 2 3 是两个 std的大小::vector< int> array_of_vectors 中:

This code works and generates the correct output (23, as 2 and 3 are the sizes of the two std::vector<int>s in array_of_vectors:

#include <array>
#include <boost/range/adaptor/transformed.hpp>
#include <iostream>
#include <vector>

using boost::adaptors::transformed;

constexpr auto get_size = [](auto const& snip){ return snip.size(); };

int main() {
    std::array<std::vector<int>,2> array_of_vectors = {
        {std::vector<int>{1,1}, std::vector<int>{1,1,1}}
    };

    auto array_of_sizes = array_of_vectors | transformed(get_size);
    for (auto i : array_of_sizes) {
        std::cout << i;
    }
}

另一方面,如果我尝试通过 boost强制 array_of_sizes 确实是 std :: array< std :: size_t,2u> ::copy_range 通过更改

On the other hand, if I try to enforce that array_of_sizes is indeed a std::array<std::size_t, 2u> via boost::copy_range by changing this

auto array_of_sizes = array_of_vectors | transformed(get_size);

对此

auto array_of_sizes = boost::copy_range<std::array<std::size_t, 2u>>(
        array_of_vectors | transformed(get_size)
        );

我收到以下错误消息,

$ g++ -std=c++17 deleteme.cpp && ./a.out 
In file included from /usr/include/boost/range/iterator_range.hpp:13,
                 from /usr/include/boost/range/adaptor/transformed.hpp:16,
                 from deleteme.cpp:2:
/usr/include/boost/range/iterator_range_core.hpp: In instantiation of ‘SeqT boost::copy_range(const Range&) [with SeqT = std::array<long unsigned int, 2>; Range = boost::range_detail::transformed_range<<lambda(const auto:1&)
 2> >]’:
deleteme.cpp:16:114:   required from here
/usr/include/boost/range/iterator_range_core.hpp:842:20: error: no matching function for call to ‘std::array<long unsigned int, 2>::array(boost::range_detail::extract_const_iterator<boost::range_detail::transformed_range<<la
y<std::vector<int>, 2> >, true>::type, boost::range_detail::extract_const_iterator<boost::range_detail::transformed_range<<lambda(const auto:1&)>, std::array<std::vector<int>, 2> >, true>::type)’
  842 |             return SeqT( boost::begin( r ), boost::end( r ) );
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from deleteme.cpp:1:
/usr/include/c++/10.2.0/array:94:12: note: candidate: ‘std::array<long unsigned int, 2>::array()’
   94 |     struct array
      |            ^~~~~
/usr/include/c++/10.2.0/array:94:12: note:   candidate expects 0 arguments, 2 provided
/usr/include/c++/10.2.0/array:94:12: note: candidate: ‘constexpr std::array<long unsigned int, 2>::array(const std::array<long unsigned int, 2>&)’
/usr/include/c++/10.2.0/array:94:12: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/10.2.0/array:94:12: note: candidate: ‘constexpr std::array<long unsigned int, 2>::array(std::array<long unsigned int, 2>&&)’
/usr/include/c++/10.2.0/array:94:12: note:   candidate expects 1 argument, 2 provided

另一方面,最令我惊讶的是(!),如果我强制 array_of_sizes std :: vector< std :: size_t> ,例如

On the other hand, most surprisingly to me (!), if I force array_of_sizes to be a std::vector<std::size_t>, e.g.

auto array_of_sizes = boost::copy_range<std::vector<std::size_t>>( // vector instead of array
        array_of_vectors | transformed(get_size)
        );

有效!看起来 transformed 可以将 std :: array< T,N> 转换为可转换为 std :: vector< T> 的范围就像它会丢失对 std :: array 的编译时大小的跟踪.

it works! It looks like transformed turns a std::array<T,N> into a range convertible to std::vector<T>, just like it loses track of std::array's compile-time size.

此错误/行为是由于什么造成的?

What is this error/behavior due to?

我只能认为由于 std :: array 的编译时已知大小而不起作用,而不是 std :: vector 的运行时间大小. transformed 可能无法解决这个问题?或者也许是 copy_range 不能?

I can only think that something is not working because of std::array's compile-time known size, as opposed to std::vector's run-time size. Probably transformed can't deal with that? Or maybe it's copy_range which can't?

推荐答案

如果使用 -std = c ++ 2a 进行编译,则gcc 10.2会显示一条错误消息:

If you compile with -std=c++2a, gcc 10.2 has a nice error message:

boost_1_74_0/boost/range/iterator_range_core.hpp:842:38: error: array must be initialized with a brace-enclosed initializer
  842 |             return SeqT( boost::begin( r ), boost::end( r ) );
      |                          ~~~~~~~~~~~~^~~~~

问题是 boost :: copy_range 仅知道如何构造将迭代器对作为其构造函数参数之一的容器,因此它与 std :: array不兼容实际上没有任何构造函数-只能作为(括号)元素列表的集合来构造.

The problem is that boost::copy_range only knows how to construct containers that take an iterator pair as (one of) their constructor parameters, so it is incompatible with std::array which does not actually have any constructors - it is only constructible as an aggregate from a (braced) list of elements.

使用C ++ 20的lambda,您可以编写一个替代函数,该函数使用简单的元编程来生成该元素列表:

With C++20's lambdas, you could write an alternate function that uses simple metaprogramming to produce that list of elements:

template<class T, class R>
T copy_range_fixed_size(R const& r) {
    return [&r]<std::size_t... I>(std::index_sequence<I...>) {
        auto it = boost::begin(r);
        return T{(I, *it++)...};
    }(std::make_index_sequence<T{}.size()>{});
}

示例.

这篇关于我可以在boost :: adaptors :: std :: array的结果上使用boost :: copy_range来返回另一个std :: array吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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