Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::add_pointer

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
       
      Defined in header<type_traits>
      template<class T>
      struct add_pointer;
      (since C++11)

      IfT is areferenceable type or (possibly cv-qualified)void, the member typedeftype provided istypenamestd::remove_reference<T>::type*.

      Otherwise, the member typedeftype provided isT.

      If the program adds specializations forstd::add_pointer, the behavior is undefined.

      Contents

      [edit]Nested types

      Name Definition
      type determined as above

      [edit]Helper types

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

      [edit]Possible implementation

      namespace detail{template<class T>struct type_identity{using type= T;};// or use std::type_identity (since C++20) template<class T>auto try_add_pointer(int)-> type_identity<typenamestd::remove_reference<T>::type*>;// usual case template<class T>auto try_add_pointer(...)-> type_identity<T>;// unusual case (cannot form std::remove_reference<T>::type*)}// namespace detail template<class T>struct add_pointer: decltype(detail::try_add_pointer<T>(0)){};

      [edit]Example

      Run this code
      #include <iostream>#include <type_traits> template<typename F,typename Class>void ptr_to_member_func_cvref_test(F Class::*){// F is an “abominable function type”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"); typedefstd::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);}

      Output:

      i = 123*pi = 123j = 123

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2101C++11the program was ill-formed ifT is afunction type withcv orrefthe type produced isT in this case

      [edit]See also

      (C++11)
      checks if a type is a pointer type
      (class template)[edit]
      removes a pointer from the given type
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/add_pointer&oldid=175317"

      [8]ページ先頭

      ©2009-2025 Movatter.jp