This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Improve diagnostics for invalid std::allocator specializations


For PR libstdc++/55963 I said I'd improve the diagnostics when you try
to use std::vector<const T>, which is invalid because the C++
Allocator requirements require the value_type to be a non-const object
type.

I tried adding static assertions to the primary std::allocator
template, but the diagnostics are still not very clear, because const
types cause ambiguous overloads and reference types cause invalid
pointer-to-reference types to be formed.

This patch adds std::allocator<const T> and std::allocator<T&> partial
specializations instead, which inherit from std::allocator<T> so they
can be used in limited ways (most importantly, you can rebind them to
a valid specialization) but attempting to construct such an allocator
will fail with a helpful message in C++11 mode (and just fail due to
using a private constructor in C++03 mode.)

The change passes testing, but I haven't committed yet as I'd like to
hear other opinions on the approach, or if this is even worth doing at
all.  I think the better diagnostics are helpful, e.g. with this patch
instantiating std::vector<const int> gives the following, rather than
33 far more cryptic lines:

In file included from /home/jwakely/gcc/4.x/include/c++/4.9.0/vector:61:0,
                 from a.cc:1:
/home/jwakely/gcc/4.x/include/c++/4.9.0/bits/allocator.h: In
instantiation of ‘std::allocator<const _Tp>::allocator() [with _Tp =
int]’:
a.cc:5:26:   required from here
/home/jwakely/gcc/4.x/include/c++/4.9.0/bits/allocator.h:134:2: error:
static assertion failed: allocator's value_type must be non-const
  static_assert( !is_const<const _Tp>::value,
  ^

Attachment: patch.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]