std::add_pointer - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::add_pointer

From cppreference.com
< cpp‎ | types

Defined in header `<type_traits>` |  |
template< class T > struct add_pointer; |  | (since C++11)

Provides the member typedef `type` which is the type `T*`. If `T` is a reference type, then `type` is a pointer to the referred type.

### [edit] Member types

Name | Definition
`type` | pointer to `T` or to the type referenced by `T`

### [edit] Helper types

template< class T > using add_pointer_t = typename add_pointer<T>::type; |  | (since C++14)

### [edit] Possible implementation

    template< class T >
    struct add_pointer {
        typedef typename std::remove_reference<T>::type* type;
    };

### [edit] Example

Run this code

    #include <iostream>
    #include <type_traits>

    int main()
    {
        int i = 123;
        int& ri = i;
        typedef std::add_pointer<decltype(i)>::type IntPtr;
        typedef std::add_pointer<decltype(ri)>::type IntPtr2;
        IntPtr pi = &i;
        std::cout << "i = " << i << "\n";
        std::cout << "*pi = " << *pi << "\n";

        static_assert(std::is_pointer<IntPtr>::value, "IntPtr should be a pointer");
        static_assert(std::is_same<IntPtr, int*>::value, "IntPtr should be a pointer to int");
        static_assert(std::is_same<IntPtr2, IntPtr>::value, "IntPtr2 should be equal to IntPtr");

        typedef std::remove_pointer<IntPtr>::type IntAgain;
        IntAgain j = i;
        std::cout << "j = " << j << "\n";

        static_assert(!std::is_pointer<IntAgain>::value, "IntAgain should not be a pointer");
        static_assert(std::is_same<IntAgain, int>::value, "IntPtr should be equal to int");
    }

Output:

    i = 123
    *pi = 123
    j = 123

### [edit] See also

is_pointer (C++11) | checks if a type is a pointer type (class template) [edit]
remove_pointer (C++11) | removes pointer from the given type (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/types/add_pointer&oldid=72248"