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
が
referenceable type
または(possibly cv-qualified)
void
である場合、提供されるメンバーtypedef
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* を形成できない場合) } // namespace 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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2101 | C++11 |
T
が
function type
で
cv
または
ref
を持つ場合、プログラムは不適格であった
|
この場合、生成される型は
T
である
|
関連項目
|
(C++11)
|
型がポインタ型かどうかをチェックする
(クラステンプレート) |
|
(C++11)
|
指定された型からポインタを除去する
(クラステンプレート) |