1

I've just seen the first part of Herb Sutter's "Writing Good C++14... By Default" (https://www.youtube.com/watch?v=hEx5DNLWGgA) and i have a (possible stupid) question regarding array_view.

The presented case was sending an array_view instead of a pointer and length in order to avoid pointer arithmetic, but how can array_view handle a case like this:

int vec[10];
func(vec+2, 5); //start from the 2nd element and process 5 of them

Does array_view support this kind of stuff or i just got the use-case wrong?

Drax
  • 12,682
  • 7
  • 45
  • 85
Andrei
  • 100
  • 2
  • 11

2 Answers2

4

First of all, you can get the slides from cppcon's Github repo.

As you can see in #8 and #10, you can write the following:

Run It Online !

// http://llvm.org/apt/
// sudo apt-get install clang-3.6 lldb-3.6 libc++-dev libc++abi-dev
// clang-3.6 -stdlib=libc++ -std=c++14 main.cpp -lc++ -lc++abi

#include <array>
#include <vector>
#include "array_view.h"  // https://github.com/Microsoft/GSL

void func(gsl::array_view<int> av)
{
    // ...
}

int main()
{
    {
        int vec[10];
        func(vec);
        //func(vec, 5);    // syntax error (func expects exactly 1 argument)
        func({vec, 5});    // notice the curly braces
        func({vec+2, 5});
    }

    {
        std::vector<int> vec;
        func(vec);
    }

    {
        size_t len = 10;
        int* p = new int[10];
        func({p,len});
        // remember to delete[] p
    }

    {
        std::array<int, 2> arr;
        func(arr);
    }
}

And that makes sense. If you look at array_view.h, you'll see all of array_view's constructors:

constexpr array_view(pointer ptr, bounds_type bounds)
constexpr array_view(T * const & data, size_type size)
constexpr array_view(T(&arr)[N], size_type size)
constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr)
// ...
maddouri
  • 3,737
  • 5
  • 29
  • 51
  • Thanks for the repo, i didn't knew about it! As I've told @Drax earlier, I see now that `func({vec+2, 5})` is the best approach, but I still consider that it would've been nice to have an offset parameter in array_view, so we can construct it in a safer manner. If the array has 3 elements and i do `func({vec+4, 5})`, I'm guessing that it will still be a runtime error, but if the call were something like `func({vec, 4, 5})`, it could have been a compile time error. – Andrei Oct 22 '15 at 09:04
  • 2
    No need to guess, you can test it online if you want ;) (cf. the "Run It Online !" button) – maddouri Oct 22 '15 at 09:11
1

With string_view you can do this:

const char* str = "hello world!"; 
func(std::experimental::string_view(str + 2, 5));

That is use one of the constructors of the view to to build it from a substring.

So with array_view you'll probably be able to do this:

int vec[10];
func(std::experimental::array_view(vec + 2, 5));

Note: There doesn't seem to be any official array_view as of c++14.

Drax
  • 12,682
  • 7
  • 45
  • 85
  • Doesn't this defy the whole purpose of array_view? As far as i understood, it is used because it's safer by giving a compile error if we cross the bounds of an array. Using it like this (array_view(vec + 2, 5)), we can cross the bounds of the array before constructing the array_view. – Andrei Oct 22 '15 at 08:36
  • @Andrei You don't gain `array_view`'s benefits before it is constructed. At the point where you're building the `array_view` you have an array so you deal with the pros and cons of an array. When inside `func` though you have a safer abstracted view of your array. – Drax Oct 22 '15 at 08:42
  • I guess you're right. It would've been nice to have an `offset` parameter in `array_view`, so we can construct it in a safely manner though. – Andrei Oct 22 '15 at 08:49