Going Beyond std::is_pointer in C++

2025-09-10

std::is_pointer is a type trait from the <type_traits> header. It's a template that provides a boolean member value, which is true if its template argument is a pointer type, and false otherwise. This check happens entirely at compile time, which is super useful for making decisions in template code.

For example, std::is_pointer<int*>::value is true, while std::is_pointer<int>::value is false.

One of the most frequent misunderstandings is that std::is_pointer doesn't work for smart pointers like std::unique_ptr or std::shared_ptr. It only checks for raw C-style pointers (int*, char*, etc.).

Problem
You try to use std::is_pointer to check for smart pointers.

Example

#include <iostream>
#include <type_traits>
#include <memory>

int main() {
    std::cout << std::boolalpha; // To print true/false instead of 1/0
    std::cout << "Is std::unique_ptr a pointer? "
              << std::is_pointer<std::unique_ptr<int>>::value << std::endl;
    // This will print "false"
    return 0;
}

Why it's a problem
The smart pointer itself is a class object, not a raw pointer. std::is_pointer correctly sees that it's a class and returns false. This can lead to unexpected behavior if your template code assumes all "pointer-like" types will be handled.

When you need to handle both raw and smart pointers, a common technique is to use type traits that check for specific member functions or nested types, or to use function overloading with SFINAE.

A more robust solution is to create your own type trait that checks for the existence of a get() member function, which is a common pattern for smart pointers. We can use std::void_t to help with this.

Goal
Create a trait that returns true for raw pointers AND types with a get() method.

Example Code

#include <iostream>
#include <type_traits>
#include <memory>

// Helper template alias
template<typename...>
using void_t = void;

// Primary template for our custom trait
template<typename T, typename = void>
struct is_pointer_or_smart : std::is_pointer<T> {};

// Specialization for types with a get() method
template<typename T>
struct is_pointer_or_smart<T, void_t<decltype(std::declval<T>().get())>> : std::true_type {};

// Another specialization for shared_ptr, just to show it works
template<typename T>
struct is_pointer_or_smart<std::shared_ptr<T>> : std::true_type {};

int main() {
    std::cout << std::boolalpha;
    std::cout << "Is int* a pointer or smart pointer? "
              << is_pointer_or_smart<int*>::value << std::endl; // true
    std::cout << "Is std::unique_ptr<int> a pointer or smart pointer? "
              << is_pointer_or_smart<std::unique_ptr<int>>::value << std::endl; // true
    std::cout << "Is std::shared_ptr<int> a pointer or smart pointer? "
              << is_pointer_or_smart<std::shared_ptr<int>>::value << std::endl; // true
    std::cout << "Is int a pointer or smart pointer? "
              << is_pointer_or_smart<int>::value << std::endl; // false
    return 0;
}

How it works

is_pointer_or_smart is our custom type trait. The first primary template inherits from std::is_pointer, so it handles raw pointers.

The second specialization uses SFINAE (Substitution Failure Is Not An Error) with void_t. The decltype(std::declval<T>().get()) part attempts to call a get() method on a hypothetical object of type T. If this call is valid, the specialization is chosen, and it inherits from std::true_type. If not, the specialization is ignored, and the primary template is used.

This approach is powerful because it's extensible and can be adapted to other "pointer-like" types that you might encounter in your code.

For more modern C++ (C++17 and later), if constexpr is a fantastic way to handle different types in a clean, readable way. You can combine it with std::is_convertible to check if a type can be converted to a raw pointer.

Goal
Write a template function that works for both raw pointers and smart pointers.

Example Code

#include <iostream>
#include <type_traits>
#include <memory>

template <typename T>
void process_pointer_like(T&& ptr) {
    if constexpr (std::is_pointer_v<T>) {
        std::cout << "Processing a raw pointer." << std::endl;
        // Your logic for raw pointers
    } else if constexpr (std::is_convertible_v<T, typename T::element_type*>) {
        std::cout << "Processing a smart pointer." << std::endl;
        // Your logic for smart pointers (e.g., ptr.get())
        // Note: This relies on the smart pointer having an 'element_type'
        // which is a common convention (e.g., std::unique_ptr<T>::element_type is T).
    } else {
        std::cout << "Not a pointer-like type." << std::endl;
    }
}

int main() {
    int* raw_ptr = new int(10);
    std::unique_ptr<int> smart_ptr = std::make_unique<int>(20);

    process_pointer_like(raw_ptr);
    process_pointer_like(smart_ptr);
    process_pointer_like(5); // A non-pointer type

    delete raw_ptr; // Don't forget to clean up raw pointers!
    return 0;
}

How it works

if constexpr performs the check at compile time. Only the code block for the true condition is compiled, which avoids compilation errors that would occur if you tried to call get() on a raw pointer, for instance.

std::is_pointer_v<T> is a convenient variable template version of std::is_pointer<T>::value.

std::is_convertible_v<T, typename T::element_type*> checks if the type T can be converted to a raw pointer of the underlying type. This works well for std::unique_ptr and std::shared_ptr.


cpp



std::is_eq vs. operator==: A C++ Comparison

std::is_eq is a template from the <utility> header in C++. It's used to compare two values, x and y, using the spaceship operator


Understanding C++'s alignas: Common Pitfalls and Alternatives

One of the most frequent problems with alignas is misunderstanding what it does. It tells the compiler to align a variable or type on a memory boundary that's a multiple of a certain value (the alignment). If you're not careful


C++ Ranges zip_transform_view::sentinel: A Deep Dive into operator==

Hey there! Let's dive into a common head-scratcher with C++ Ranges, specifically the operator== for a ranges::zip_transform_view::sentinel



C++ Copying in Reverse: A Guide to std::ranges::copy_backward

A common issue with std::ranges::copy_backward is getting the destination iterator wrong. This function requires a past-the-end iterator for the destination range


Understanding union in C++: Problems with Type Safety and The std::variant Solution

In C++, a union is a special kind of class that allows you to store different data types in the same memory location. Think of it like a shared locker with multiple compartments


The Modern C++ Way: Replacing std::time_put for Time Formatting

At its core, std::time_put is a facet of the C++ locale library. A facet is a class that provides a specific behavior for a locale


Understanding Allocator Propagation in C++ priority_queue

The topic focuses on the std::uses_allocator type trait when applied to std::priority_queue. I'll break down what this means


The Dangers of std::fclose and Why is Your Best Friend

std::fclose is a C-style function that's part of the <cstdio> library in C++. It's used to close a file that was opened using std::fopen


Mastering C++ std::array: Troubleshooting and Alternatives

Sizing and Initialization IssuesOne of the biggest gotchas with std::array is that its size must be a compile-time constant


Mastering std::ranges::contains: Common Issues & Alternatives

The std::ranges::contains function is quite straightforward, but there are a few things that can trip you up.Header Inclusion The most basic issue is often forgetting to include the right header