Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why couldn't I get the size of a range in range-v3?

I want to get the number of people whose name starts with 'T':

#include <iostream>
#include <string>
#include <range\v3\all.hpp>

using namespace ranges;

int main()
{
    const auto names = std::vector<std::string> {"Tony", "Peter"};

    std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
}

But I got huge compile error:

λ clang -std=c++14 test.cpp
test.cpp:11:18: error: no matching function for call to object of type 'const ranges::v3::adl_size_detail::size_fn'
std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
             ^~~~
K:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\range/v3/size.hpp:90:32: note: candidate template
  ignored: substitution failure [with Rng =
  ranges::v3::remove_if_view<ranges::v3::iterator_range<std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,
  std::char_traits<char>, std::allocator<char> > > > >,
  std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char, std::char_traits<char>,
  std::allocator<char> > > > > >, ranges::v3::logical_negate<(lambda at test.cpp:11:44)> >]: no matching function
  for call to 'size'
            constexpr auto operator()(Rng &&rng) const ->
                           ^
K:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\range/v3/utility/iterator.hpp:405:32: note: candidate
  function template not viable: requires 2 arguments, but 1 was provided
        iterator_size_t<I> operator()(I begin, S end) const

By the way, I use the clang 3.7 in the Visual Studio 2015 Update 1. So, what's wrong?

like image 270
Cu2S Avatar asked Mar 20 '16 09:03

Cu2S


People also ask

Is Range v3 header only?

The library used in the code examples is not really the C++20 ranges, it's the ranges-v3 open-source library from Eric Niebler, which is the basis of the proposal to add ranges to the C++. It's a header-only library compatible with C++11/14/17.

What is Range v3?

Range v3 is a generic library that augments the existing standard library with facilities for working with ranges. A range can be loosely thought of a pair of iterators, although they need not be implemented that way.


1 Answers

You're better off with the count_if algorithm.

auto cnt = ranges::count_if( names, [](const auto& s) {return s[0] == 'T';} )
like image 54
Eric Niebler Avatar answered Oct 18 '22 20:10

Eric Niebler