Is it possible to customize the reference
of a std::vector
.
Until C++11 it seemed to be possible through the Allocator
template parameter.
But not anymore?
According to the documentation, http://en.cppreference.com/w/cpp/container/vector, reference
is now always value_type
and value_type
is always the template parameter T
.
It seems to be impossible even using allocator_traits
,
http://en.cppreference.com/w/cpp/memory/allocator_traits
Is there a workaround for this?
If not, does it means that I have to specialize the entire std::vector
and probably reproduce all its functionality if I want to have a special reference
type based on the allocator?
If this is so, generally speaking what is the logic for all these constrains? To force the user to use std::vector<T, A>
to always manage regular memory (in which, value_type = T
, reference = T&
and pointer = T*
?)
Note: I am aware of the std::vector<bool>
controversy. However this a bit more general because in principle I need a custom std::vector<RegularType, special_allocator<RegularType>>
mainly to control the return type of operator[](int)
to a special proxy object.
Specific implementation: I am looking at GCC 6.3.1's stdlib source and in std_vector.h
one can read:
template<typename _Tp, ...>
class vector : ...{
...
public:
typedef typename _Alloc_traits::reference reference;
};
which seems to indicate that it is still possible to specify a reference type via the allocator
(_traits
?).
According to the documentation, allocator
or the allocator_traits
doesn't need to have a reference
type.
Plus, I don't know how to customize this type anyway.
Is GCC not following the standard? Or is it simply that indirectly allocator_traits<Allocator>::reference
is forced to be allocator_traits<Allocator>::value_type&
?
std::vector
, it seems that one has to roll out a complete reimplementation of the class. – Tremolite