c++ - Testing if std::pointer_traits can work with my type -


how can check @ compile-time whether or not arbitrary type can used std::pointer_traits? had hoped simple sfinae solution might work:

template <typename t, typename = void> struct pointer_traits_ready : std::false_type {};  template <typename t> struct pointer_traits_ready<          t,          std::void_t<typename std::pointer_traits<t>::element_type>        > : std::true_type {};  static_assert(!pointer_traits_ready<int>::value,""); 

...but invokes static assert within standard library (ptr_traits.h). std::is_pointer doesn't accommodate smart pointers.

you can't. [pointer.traits.types]:

using element_type = see below ; 

type: ptr::element_type if qualified-id ptr::element_type valid , denotes type (14.8.2); otherwise, t if ptr class template instantiation of form somepointer<t, args>, args 0 or more type arguments; otherwise, specialization ill-formed.

in order sfinae-friendly, need pointer_traits<foo> lack type alias named element_type. problem is, element_type specified being ill-formed - not absent. cannot use pointer_traits detector whether or not can used pointer type.

even if wrote own type sfinae-friendly version of specification, wouldn't able catch user specializations of pointer_traits own type. sad panda.


Comments