Why std::valarray
does not support custom allocators? How designed its memory management? Is there new
-based or malloc
-based allocator used?
All other containers generally provide a possibility to specify custom allocator. Say, std::vector
in libstdc++
have corresponding template parameter Allocator
and I can specify __gnu_cxx::__mt_alloc
as allocator.

- 15,657
- 10
- 64
- 169
1 Answers
Why std::valarray does not support custom allocators?
Because std::valarray
is not a dynamic array (Like std::vector
). Its a kind of optimized version of vector for numeric computation, designed to rely on SIMDs and similar optimizations.
From the documentation:
std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword restrict in the C programming language. In addition, functions and operators that take valarray arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression [...] avoiding any temporaries or multiple passes.
As you can see, its not a simple dynamic array. Its a dynamic array designed to hold numeric values and do vectorized computations over them, via many optimizations.

- 13,969
- 4
- 40
- 75