Why does boost :: multi_array ConstMultiArrayConcept contain a NumDims template argument?

I wrote an operator<< specialization that handles boost::multi_array and uses ConstMultiArrayConcept to work with both an external array and subarrays. However, I wonder why the concepts of multi_array have the template argument std::size_t NumDims , since it can simply be extracted from multi_array . The only use of NumDims in ConstMultiArrayConcept is the recursion depth argument for idgen_helper , which checks slicing.

For reference, here is the header for multi_array : http://www.boost.org/doc/libs/1_51_0/boost/multi_array/concept_checks.hpp

And here is my overloaded operator<<

 template <typename CharT, typename Traits, typename MultiArrayT> BOOST_CONCEPT_REQUIRES( ((boost::multi_array_concepts::ConstMultiArrayConcept<MultiArrayT, MultiArrayT::dimensionality>)), (std::basic_ostream<CharT, Traits>&)) // return type operator <<( std::basic_ostream<CharT, Traits>& os, MultiArrayT const& ary ) { typename std::basic_ostream<CharT, Traits>::sentry opfx( os ); if ( opfx ) { boost::multi_array_types::size_type const* sizes = ary.shape(); // using Mathematica array notation os << "{"; for ( int i = 0; i < sizes[0]; ++i ) { if ( i > 0 ) os << ", "; // verbose just to keep the types apparent typedef typename MultiArrayT::const_reference subType; subType item = ary[i]; os << item; } os << "}\n"; } return os; } 

This specialization works, but I have to miss something in my understanding. Any clues would be appreciated.

+8
c ++ boost c ++ - concepts boost-multi-array
source share
1 answer

Concept class template declaration:

 template <typename Array, std::size_t NumDims> struct ConstMultiArrayConcept { ... }; 

See how ConstMultiArrayConcept really used in Boost code:

  template <typename T, std::size_t NumDims> class multi_array_ref { ... // Assignment from other ConstMultiArray types. template <typename ConstMultiArray> multi_array_ref& operator=(const ConstMultiArray& other) { function_requires< detail::multi_array:: ConstMultiArrayConcept<ConstMultiArray,NumDims> >(); ... 

The same code in multi_array_view& operator=() and sub_array& operator=() , which takes a different type of ConstMultiArray .

It seems that NumDims not the dimension number of the passed Array array type, it is the dimension number of the external array, which checks the assignment compatibility with this other array type. Therefore, it could not be deduced from the Array template parameter.

+1
source share

All Articles