Like std::is_pointer and std::is_integral, how to implement std::is_cstring_pointer?

Viewed 153

Like std::is_pointer and std::is_integral, how to implement std::is_cstring_pointer?

I want to check the pointer is a c type string pointer or not, is it possible?

How to achieve this goal?

1 Answers

It is fairly simple to combine the existing type traits:

template <class T>
struct is_char_ptr
    : std::integral_constant<bool,
        std::is_pointer_v<T>
        && std::is_same_v<char,
          typename std::remove_cv_t<std::remove_pointer_t<T>>>>
{};

Note that a pointer to char may point to a null terminated string, or it might not. Whether it does will not affect the type.

Related