eclipse / omr

Eclipse OMR™ Cross platform components for building reliable, high performance language runtimes
http://www.eclipse.org/omr
Other
945 stars 396 forks source link

Automatically detect constness issues in allocator types #1758

Open mgaudet opened 7 years ago

mgaudet commented 7 years ago

1712 (derived from #1707 ) points out that the allocator types we've used before are subtly not const-correct, but accepted by most of our build compilers.

Nevertheless, we should detect when we get this wrong.

This might just be easily solved by having advanced toolchain builds where we build with Clang 5+ and GCC 7+ to detect early changes.

rwy7 commented 6 years ago

What about if we implemented some helper types to make this easier to use:

template <class K, class V, class A = RawAllocator, class C = std::less<const K>>
typedef std::map<K, V, C, typed_allocator<std::pair<const K, V>, A>> Map;

template <class K, class V, class C = std::less<const K>> 
typedef Map<K, V, C, RegionAllocator> RegionAllocatedMap;

Or even just a thin wrapper type that has the assert built in:

template <K, V, C, A>
struct Map : public std::map<K, V, C, A> {
  using std::map::map;
  static_assert(std::is_same<value_type, A::value_type>::value,
    "Allocator's value_type must match map's value_type");
};