首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::add_pointer

Defined in header <type_traits>

template< class T > struct add_pointer;

(since C++11)

如果T是一个引用类型,然后提供成员类型type它是指向引用类型的指针。

否则,如果T为对象类型命名,则不属于cv或ref限定%28的函数类型(因为C++17%29)或%28(可能是cv限定%29空类型)提供了type哪种类型T*...

Otherwise (if T is a cv- or ref-qualified function type), provides the member typedef type which is the type T.

(since C++17)

成员类型

Name

Definition

type

pointer to T or to the type referenced by T

帮助者类型

template< class T > using add_pointer_t = typename add_pointer<T>::type;

(since C++14)

可能的实施

命名空间详细信息{Template<class T,bool是[医]功能[医]type=false>structadd[医]指针{使用type=type Name std::Remove[医]参照系<T>*类型%2A模板<class T>结构添加[医]指针<T,true>{使用type=T;};模板<类T,类...。args>structadd[医]指针<T%28 Args...%29,true>{使用类型=T%28%2A%29%28 Args...%29;};模板<T类,类...。args>structadd[医]指针<T%28 Args...,...%29,true>{使用类型=T%28%2A%29%28 Args...,...%29;};}/命名空间详细模板<class T>结构添加[医]指针:详细信息::添加[医]指针<T,std::is[医]功能<T>*价值>{};

*。

二次

代码语言:javascript
复制
#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, "IntAgain should be equal to int");
}

二次

产出:

二次

代码语言:javascript
复制
i = 123
*pi = 123
j = 123

二次

另见

is_pointer (C++11)

checks if a type is a pointer type (class template)

remove_pointer (C++11)

removes pointer from the given type (class template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券