Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create typedef for ``boost::array_view`` in a templated class

I want to have a dimensional agnostic template (usefull for both 3d and 4d), most of operations will be performed on submatrices with first dimension stripped.

So here is what I'd like to have

template <typename element, int dimensions>
class MMapMatrixTemplate{
public:
    typedef boost::multi_array_ref<element, dimensions> array_type; 
    typedef std::array<size_t, dimensions> index_type;
    typedef array_type::array_view<dimensions-1>::type stride_type;
};

Where array_type defines array that is managed by this class index_type defines type used to index arrays, and I'd like `stride_type to define a slice of this array having one less dimension.

For now I get an error:

  include/MMapMatrix.hh:31:55: error: non-template ‘array_view’ used as template
   typedef boost::multi_array_ref<element, dimensions>::array_view<dimensions-1>::type stride_type;
                                                   ^
like image 222
jb. Avatar asked Dec 17 '25 13:12

jb.


2 Answers

From the documentation on views, you can see the definition of the view type as:

  typedef typename Array::template array_view<3>::type view1_t;

so this makes your code compile:

#include "boost/multi_array.hpp"

template <typename element, int dimensions>
class MMapMatrixTemplate{
public:

    typedef boost::multi_array_ref<element, dimensions> array_type; 

    typedef std::array<size_t, dimensions> index_type;

    //typedef array_type::array_view<dimensions-1>::type stride_type;
    typedef typename array_type::template array_view<dimensions-1>::type stride_type;
};

int main(int argc, const char *argv[])
{

    typedef MMapMatrixTemplate<double, 4> matrix;

    return 0;
}

You need to specify that the array_view is actually a class template in order to use it as such. Otherwise, the compiler expects it to be fully defined type.

like image 185
tmaric Avatar answered Dec 20 '25 11:12

tmaric


You need typename and/or .template qualification on dependent names:

typedef typename array_type::array_view<dimensions-1>::type stride_type;

If you're using a template member on a dependent name, you need .template qualification:

obj.template foo<T>();

See this very popular answer for background

Where and why do I have to put the "template" and "typename" keywords?

like image 26
sehe Avatar answered Dec 20 '25 09:12

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!