[Bug libstdc++/55963] std::vector<const T> fails

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun Jan 13 22:44:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55963

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-13 22:43:56 UTC ---
Looks like PJP's comment was from 2008, so I'm not sure what requirements he
means were standardized in 2008.

Looking further into it, I see that the Allocator requirements in C++11 say an
allocator can only be instantiated with a non-const type, see
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#274

Because vector<const T> instantiates allocator<const T> that makes it
definitely ill-formed.

So I think I'm going to add a static_assert to std::allocator that rejects
const types, so the error is at least clear it's intentional not a bug.

To make it work you could write your own thin wrapper around std::allocator
that prevents instantiating it with const types, something like:

template<typename T>
struct allocator
  : std::allocator<typename std::remove_const<T>::type>
{
  template<typename U>
  struct rebind { typedef allocator<U> other; };

  typedef T value_type;

  allocator() = default;

  allocator(const allocator&) = default;

  template<typename U>
    allocator(const allocator<U>&) { }
};

Then use std::vector<const int, allocator<const int>>.



More information about the Gcc-bugs mailing list