Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::function<R(Args...)>::target

      From cppreference.com
      <cpp‎ |utility‎ |functional‎ |function
       
       
      Utilities library
       
      Function objects
      Function invocation
      (C++17)(C++23)
      Identity function object
      (C++20)
      Old binders and adaptors
      (until C++17*)
      (until C++17*)
      (until C++17*)
      (until C++17*)  
      (until C++17*)
      (until C++17*)(until C++17*)(until C++17*)(until C++17*)
      (until C++20*)
      (until C++20*)
      (until C++17*)(until C++17*)
      (until C++17*)(until C++17*)

      (until C++17*)
      (until C++17*)(until C++17*)(until C++17*)(until C++17*)
      (until C++20*)
      (until C++20*)
       
       
      template<class T>
      T* target()noexcept;
      (1)(since C++11)
      template<class T>
      const T* target()constnoexcept;
      (2)(since C++11)

      Returns a pointer to the stored callable function target.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      A pointer to the stored function iftarget_type()==typeid(T), otherwise a null pointer.

      [edit]Example

      Run this code
      #include <functional>#include <iostream> int f(int,int){return1;}int g(int,int){return2;}void test(std::function<int(int,int)>const& arg){std::cout<<"test function: ";if(arg.target<std::plus<int>>())std::cout<<"it is plus\n";if(arg.target<std::minus<int>>())std::cout<<"it is minus\n"; int(*const* ptr)(int,int)= arg.target<int(*)(int,int)>();if(ptr&&*ptr== f)std::cout<<"it is the function f\n";if(ptr&&*ptr== g)std::cout<<"it is the function g\n";} int main(){    test(std::function<int(int,int)>(std::plus<int>()));    test(std::function<int(int,int)>(std::minus<int>()));    test(std::function<int(int,int)>(f));    test(std::function<int(int,int)>(g));}

      Output:

      test function: it is plustest function: it is minustest function: it is the function ftest function: it is the function g

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2591C++11the behavior is undefined ifT is notCallablebehavior is defined (always returnsnullptr)

      [edit]See also

      obtains thetypeid of the stored target
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/function/target&oldid=154452"

      [8]ページ先頭

      ©2009-2025 Movatter.jp