Search code examples
c++boostboost-ublas

identity_matrix/zero_matrix: do they allocate?


matrix classes identity_matrix and zero_matrix are templates with ALLOC as a second parameter. But do they really allocate memory?


Solution

  • No, they don't allocate memory, as can be seen here and here. I think the documentation is misleading: allocators are not used for initialization of static zero_ or one_ elements, just the constuctors of the type T:

    template<class T, class ALLOC>
    const typename zero_matrix<T, ALLOC>::value_type zero_matrix<T, ALLOC>::zero_ = T(/*zero*/);
    
    ...
    template<class T, class ALLOC>
    const typename identity_matrix<T, ALLOC>::value_type identity_matrix<T, ALLOC>::zero_ = T(/*zero*/);
    template<class T, class ALLOC>
    const typename identity_matrix<T, ALLOC>::value_type identity_matrix<T, ALLOC>::one_ (1); // ISSUE: need 'one'-traits here
    

    However, the typedefs size_type and difference_type are part of the public interface and in order to be consistent, ALLOC::size_type and ALLOC::difference_type are used (instead of the "usual" std::size_t and std::ptrdiff_t). This was done with the following change.