gslcpp
Modern-C++ Wrapper for GSL

The template-class gsl::vector_view is one of two descendants of gsl::v_iface.

The other is gsl::vector.

gsl::vector_view exists to provide constructors for gsl::v_iface.

  • gsl::vector_view provides no interface other than construction.
  • gsl::v_iface provides almost all of the interfaces that GSL's native vector provides.
  • An instance constructed via one of the template-specializations of gsl::vector_view does not own memory allocated for elements in a vector.
  • Internally, the return-value of gsl_vector_view_array() is stored on the stack in the instance of gsl::vector_view.
    • The instance of gsl_vector_view returned by gsl_vector_view_array() consumes about 40 bytes on a 64-bit machine.
  • There is on the stack no pointer to the virtual-function table.
    • There is no virtual destructor in the inheritance-chain.

Example

The template-parameters to gsl::vector_view often need not be specified because they are deduced by the compiler.

Here is an example-program:

using std::cout;
using std::endl;
// Example-usage of gsl::vector_view.
int main() {
complex<float> const a1[]= {{1.0f, -1.0f}, {0.5f, 0.6f}};
// Deduce vector_view<complex<float>,2>.
vector_view v1= a1;
cout << "\n"
<< "Instance of gsl_vector_view consumes 40 bytes on 64-bit hardware.\n"
<< "sizeof(v1)=" << sizeof(v1) << " v1=" << v1 << "\n"
<< "v1.get(1)=" << v1.get(1) << endl;
float a4[]= {1, 2, 3, 4, 5, 6};
// Deduce vector_view<float,0>, and set size to four. The output contains
// a subvector of v4: length 2, starting at offset 1 into v4, and with stride
// 2 relative to v4.
vector_view v4(a4, 4);
cout << "\n"
<< "sizeof(v4)=" << sizeof(v4) << " v4=" << v4 << "\n"
<< "v4.subvector(2, 1, 2)=" << v4.subvector(2, 1, 2) << endl;
short a5[]= {1, 2, 3, 4, 5, 6};
// Deduce vector_view<short,0>, set size to 3, and set stride to 2.
vector_view v5(a5, 3, 2);
cout << "\n"
<< "sizeof(v5)=" << sizeof(v5) << " v5=" << v5 << "\n"
<< "v5.max_index()=" << v5.max_index() << endl;
}

And here is its output:

Instance of gsl_vector_view consumes 40 bytes on 64-bit hardware.
sizeof(v1)=40 v1=[(1,-1),(0.5,0.6)]
v1.get(1)=(0.5,0.6)
sizeof(v4)=40 v4=[1,2,3,4]
v4.subvector(2, 1, 2)=[2,4]
sizeof(v5)=40 v5=[1,3,5]
v5.max_index()=2
vector-view.hpp
Definition for gsl::vector_view.
gsl::v_iface::get
T get(size_t i) const
Read element with bounds-checking.
Definition: v-iface.hpp:116
gsl::complex
Complex-number, each of whose components has type A.
Definition: complex.hpp:18
gsl::vector_view
Constructor-type for vector-by-reference whose storage is not owned by instance of view.
Definition: vector-view.hpp:64