std:: add_pointer
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct add_pointer ; |
(C++11 起) | |
如果
T
是
可引用类型
或(可能带有 cv 限定符的)
void
,则提供的成员类型定义
type
为
typename
std::
remove_reference
<
T
>
::
type
*
。
否则,提供的成员 typedef
type
为
T
。
如果程序为
std::add_pointer
添加特化,则行为未定义。
目录 |
嵌套类型
| 名称 | 定义 |
type
|
按上述方式确定 |
辅助类型
|
template
<
class
T
>
using add_pointer_t = typename add_pointer < T > :: type ; |
(C++14 起) | |
可能的实现
namespace detail { template<class T> struct type_identity { using type = T; }; // 或使用 std::type_identity (C++20 起) template<class T> auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>; // 常规情况 template<class T> auto try_add_pointer(...) -> type_identity<T>; // 特殊情况(无法构成 std::remove_reference<T>::type*) } // 命名空间 detail template<class T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {}; |
示例
#include <iostream> #include <type_traits> template<typename F, typename Class> void ptr_to_member_func_cvref_test(F Class::*) { // F 是一种“可憎的函数类型” using FF = std::add_pointer_t<F>; static_assert(std::is_same_v<F, FF>, "FF should be precisely F"); } struct S { void f_ref() & {} void f_const() const {} }; 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_v<IntPtr>, "IntPtr should be a pointer"); static_assert(std::is_same_v<IntPtr, int*>, "IntPtr should be a pointer to int"); static_assert(std::is_same_v<IntPtr2, IntPtr>, "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_v<IntAgain>, "IntAgain should not be a pointer"); static_assert(std::is_same_v<IntAgain, int>, "IntAgain should be equal to int"); ptr_to_member_func_cvref_test(&S::f_ref); ptr_to_member_func_cvref_test(&S::f_const); }
输出:
i = 123 *pi = 123 j = 123
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 2101 | C++11 |
当
T
是带有
cv
或
ref
的
函数类型
时程序非良构
|
此种情况下生成的类型为
T
|
参见
|
(C++11)
|
检查类型是否为指针类型
(类模板) |
|
(C++11)
|
从给定类型中移除指针
(类模板) |