Container with proxy iterator/reference and auto
c++, c++11, c++14, generic-programming, iterator
Solution
Proxies and `auto` don't interact well, precisely because `auto` reveals things about the types that were supposed to stay hidden.
There have been some requests for interest for `operator auto`-style things (basically, "when deducing me as a type, use this type instead"), but AFAIK none of them even made it to an official proposal.
The other problem is that `vector<bool>` is unexpected because it's the only instantiation of `vector` that uses proxies. There have been other preliminary proposals calling for `vector<bool>` to be deprecated and eventually revert to being non-special, with a special-purpose `bitvector` class introduced to take its place.
Problem
I'm implementing a container with a proxy iterator/reference type similar to `std::vector<bool>` and clash into the following issue, which I proceed to exemplify with `std::vector<bool>` (this question is not about `std::vector<bool>`!): ``` #include <vector> #include <type_traits> int main() { using namespace std; vector<bool> vec = {true, false, true, false}; auto value = vec[2]; // expect: "vector<bool>::value_type" const auto& reference = vec[2]; // expect: "vector<bool>::const_reference" static_assert(is_same<decltype(value), vector<bool>::value_type>::value, "fails: type is vector<bool>::reference!"); static_assert(is_same<decltype(reference), vector<bool>::const_reference>::value, "fails: type is const vector<bool>::reference&!"); /// Consequence: auto other_value = value; other_value = false; assert(vec[2] == true && "fails: assignment modified the vector"); ``` Is there a way to implement a proxy type such that both static assert's pass? Are there any guidelines about how to deal with this issue when implementing such a container? Maybe by using a conversion operator to `auto`/`auto&`/`auto&&`/`const auto...`? EDIT: reworked the example to make it more clear. Thanks to @LucDanton for his comment below.