Using gsl::span with range-v3

Viewed 1582

I tried a little example to get used to the GSL and range-v3 libraries and I wondered 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 works. But if I try to use gsl::span<int> as a range it leads to an error message. The compiler tells me that span does not fullfill the view concept.

#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 it should since a span is a particular view and even has begin() and end() iterators (of the same type).

  • Wouldn't it be cool if they work together being composable or are there any reasons for both to be not compatible?
  • I think this is an issue which comes from the strong "concept" usage in range-v3. Does it get automatically solved if some other sort of concept feature is supported by the language?
  • I assume span currently needs some adaption if I would like to use both libraries together in some piece of (non-industrial) software. What should I change to make these work together? (if it is a good idea at all)
  • That also leads me ultimately to the question of "What has a class to fullfill to work with range-v3?" Is inheritance from facades, adaptors or such the only way to tell the compiler currently about these conceptional requirements?
1 Answers
Related