Using gsl :: span with range-v3

I tried a small example to get used to the GSL and range-v3 libraries, and I thought how they could work together. I have this toy example

#include <iostream> #include <range/v3/all.hpp> using namespace std; using namespace ranges; void example_vector(vector<int> const& v) { ranges::for_each(view::tail(v), [](int x){ cout << x << ' '; }); cout << '\n'; } int main() { auto seq = vector<int> { 2,2,2,0,0,2,1,2 }; example_vector(seq); } 

which is working. But if I try to use gsl::span<int> as a range, this will result in an error message. The compiler tells me that span does not populate the concept of presentation.

 #include <gsl.h> // ... void example_span(gsl::span<const int> v) { ranges::for_each(view::tail(v), [](int x){ cout << x << ' '; }); cout << '\n'; } 

Compiler Message:

 note: candidate template ignored: disabled by 'enable_if' [with Rng = gsl::span<const int, -1> &, Rest = <>, _concept_requires_123 = 42] CONCEPT_REQUIRES_(ViewConcept<Rng, Rest...>())> 

But in my understanding, this should be, because span is a specific view and even has iterators begin() and end() (of the same type).

  • Would it be great if they worked together, being composed, or are there any reasons for both of them to be incompatible?
  • I think this is a problem that comes from using a strong โ€œconceptโ€ in the -3 range. Does this happen automatically if any other kind of feature feature is supported by the language?
  • I assume that span currently needs some adaptation if I want to use both libraries in some piece of (non-industrial) software. What should I change so that they work together? (if it's a good idea at all)
  • It also leads me ultimately to the question, โ€œWhat does the class have for fullfill to work with range-v3 ?โ€ Is it inherited from facades, adapters, or is that the only way to tell the compiler about these conceptual requirements?
+5
source share
1 answer

The View concept in range-v3 (and TS ranges, for that matter) requires that type R satisfy both the Range concept - begin(r) and end(r) limit the range of iterators - - and the Semiregular - R concept must be copied / moved constructively copy / move assignable and by default constructive. The iterator and sentinel types of a Range (that begin and end return) must also be Semiregular (among other requirements).

The span family does not satisfy the concept of View , since span are not constructive by default in some cases, and their iterators are not constructive by default in any case. Since even Standard C ++ requires a default build for Forward iterators, the current span iterators do not match either TS, range-v3, or Standard C ++ ranges.

However, the changes necessary to satisfy all of these families of requirements are minimal and direct .

20161207 Update:

range-v3 now contains a span implementation that correctly models the concepts of View / Range .

20170128 Update:

gsl::span now has constructive iterators by default. Therefore, gaps can now be used with the -v3 range. A space with a dynamic extent (for example, gsl::span<int> ) models the concepts of Range and View , covers only a static degree (for example, gsl::span<int, 42> ) only Range , since they do not satisfy the View requirements for building default.

+7
source

Source: https://habr.com/ru/post/1245818/


All Articles