Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.


// Copyright 2002 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software 
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at

//  Boost.MultiArray Library
//  Authors: Ronald Garcia
//           Jeremy Siek
//           Andrew Lumsdaine
//  See for documentation.

// slice.cpp - testing out slicing on a matrices

#include "generative_tests.hpp"
#include "boost/array.hpp"
#include "boost/mpl/if.hpp"
#include "boost/type_traits/is_same.hpp"

template <typename Array>
struct view_traits_mutable {
#if 0 // RG - MSVC can't handle templates nested in templates. Use traits
  typedef typename Array::template array_view<3>::type array_view3;
  typedef typename Array::template array_view<2>::type array_view2;
  typedef typename boost::array_view_gen<Array,3>::type array_view3;
  typedef typename boost::array_view_gen<Array,2>::type array_view2;

template <typename Array>
struct view_traits_const {
#if 0 // RG - MSVC can't handle templates nested in templates. Use traits
  typedef typename Array::template const_array_view<3>::type array_view3;
  typedef typename Array::template const_array_view<2>::type array_view2;
  typedef typename boost::const_array_view_gen<Array,3>::type array_view3;
  typedef typename boost::const_array_view_gen<Array,2>::type array_view2;

// Meta-program selects the proper view_traits implementation.
template <typename Array, typename ConstTag>
struct view_traits_generator :
  boost::mpl::if_< boost::is_same<ConstTag,const_array_tag>,
                   view_traits_mutable<Array> >

template <typename Array, typename ViewTraits>
void test_views(Array& A, const ViewTraits&) {
  typedef typename Array::index index;
  typedef typename Array::index_range range;
  typename Array::index_gen indices;

  const index idx0 = A.index_bases()[0];
  const index idx1 = A.index_bases()[1];
  const index idx2 = A.index_bases()[2];

  // Standard View
    typename ViewTraits::array_view3 B = A[
    for (index i = 0; i != 2; ++i)
      for (index j = 0; j != 2; ++j)
        for (index k = 0; k != 2; ++k) {
          BOOST_CHECK(B[i][j][k] == A[idx0+i][idx1+j+1][idx2+k*2]);
          boost::array<index,3> elmts;
          elmts[0]=i; elmts[1]=j; elmts[2]=k;
          BOOST_CHECK(B(elmts) == A[idx0+i][idx1+j+1][idx2+k*2]);
  // Degenerate dimensions
    typename ViewTraits::array_view2 B =
    for (index i = 0; i != 2; ++i)
      for (index j = 0; j != 2; ++j) {
        BOOST_CHECK(B[i][j] == A[idx0+i][idx1+1][idx2+j*2]);
        boost::array<index,2> elmts;
        elmts[0]=i; elmts[1]=j;
        BOOST_CHECK(B(elmts) == A[idx0+i][idx1+1][idx2+j*2]);

template <typename Array>
void access(Array& A, const mutable_array_tag&) {

  typedef typename view_traits_generator<Array,mutable_array_tag>::type

  typedef typename view_traits_generator<Array,const_array_tag>::type


  const Array& CA = A;

template <typename Array>
void access(Array& A, const const_array_tag&) {
  typedef typename view_traits_generator<Array,const_array_tag>::type

int test_main(int,char*[]) {
  return run_generative_tests();