2

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

Andrei R.
  • 2,374
  • 1
  • 13
  • 27
  • 1
    Have a look at online doc.: [boost::numeric::ublas::identity_matrix< T, ALLOC > Class Template Reference](http://www.boost.org/doc/libs/1_54_0/libs/numeric/ublas/doc/html/classboost_1_1numeric_1_1ublas_1_1identity__matrix.html#_details): _ALLOC an allocator for storing the zeros and one elements. **By default, a standar allocator is used.**_ (Sorry, for the typo. I cited by copy/paste. Emphasizing by me.) – Scheff's Cat Dec 11 '17 at 06:39
  • @Scheff, well, I looked in the code, but didn't find out why `ALLOC` is paramethrized as it is not used. – Andrei R. Dec 11 '17 at 07:38
  • I remember that once somebody told me: "Source code is the only true documentation." (and didn't write any comments in the sources/headers...) ;-) – Scheff's Cat Dec 11 '17 at 07:51

1 Answers1

2

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.

ead
  • 32,758
  • 6
  • 90
  • 153
  • so, `ALLOC` is only used for `size_type` and `difference_type`, but not for allocations. Thank you, thats what I was looking for – Andrei R. Dec 11 '17 at 07:35