identity_matrix/zero_matrix:他们分配了吗?

identity_matrix/zero_matrix: do they allocate?

matrix 类 identity_matrixzero_matrix 是将 ALLOC 作为第二个参数的模板。但是他们真的分配内存吗?

不,它们不分配内存,可以看出here and here. I think the documentation is misleading: allocators are not used for initialization of static zero_ or one_个元素,只是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

然而,typedefs size_typedifference_type 是 public 接口的一部分,为了保持一致,使用了 ALLOC::size_typeALLOC::difference_type (而不是 "usual" std::size_tstd::ptrdiff_t)。这是通过以下 change.

完成的