Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if std::pointer_traits can work with my type

How can I check at compile-time whether or not an arbitrary type can be used with std::pointer_traits? I had hoped a 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 this invokes a static assert from within the standard library (ptr_traits.h). Obviously std::is_pointer doesn't accommodate smart pointers.

like image 229
user2023370 Avatar asked Oct 25 '25 21:10

user2023370


1 Answers

You can't. From [pointer.traits.types]:

using element_type = see below ;

Type: Ptr::element_type if the qualified-id Ptr::element_type is valid and denotes a type (14.8.2); otherwise, T if Ptr is a class template instantiation of the form SomePointer<T, Args>, where Args is zero or more type arguments; otherwise, the specialization is ill-formed.

In order to be SFINAE-friendly, we would need pointer_traits<Foo> to simply lack a type alias named element_type. The problem is, element_type is specified as being ill-formed - not absent. So you simply cannot use pointer_traits as a detector for whether or not something can be used as a pointer type.

Even if you wrote your own type that was a SFINAE-friendly version of that specification, you wouldn't be able to catch user specializations of pointer_traits for their own type. Sad panda.

like image 57
Barry Avatar answered Oct 27 '25 10:10

Barry