How to allow an additional nested type like std :: allocator_traits?

At the distributor may optionally be nested types, such as pointer, const_pointer. But you can always use this interface with std::allocator_traits<Allocator>, which would provide a default version of these types if they are not in Allocator.

How is it implemented std::allocator_traits? How can a template select the default version of a nested type when it is missing?

+4
source share
1 answer

, T::pointer , , , . SFINAE, , " ". , , SFINAE .

, , idiom void_t :

template<typename T>
  using void_t = void;

template<typename T, typename = void>
  struct get_pointer
  {
    using type = typename T::value_type*;
  };

template<typename T>
  struct get_pointer<T, void_t<typename T::pointer>>
  {
    using type = typename T::pointer;
  };

, A, typename get_pointer<A>::type A::pointer, , A::value_type*

, , A::pointer , , , . A::pointer , , .

+10

Source: https://habr.com/ru/post/1568170/


All Articles