Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mem_fn

      From cppreference.com
      <cpp‎ |utility‎ |functional
       
       
      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*)
       
      Defined in header<functional>
      template<class M,class T>
      /* unspecified */ mem_fn( M T::* pm)noexcept;
      (since C++11)
      (constexpr since C++20)

      Function templatestd::mem_fn generates wrapper objects for pointers to members, which can store, copy, and invoke apointer to member. Both references and pointers (including smart pointers) to an object can be used when invoking astd::mem_fn.

      Contents

      [edit]Parameters

      pm - pointer to member that will be wrapped

      [edit]Return value

      std::mem_fn returns a call wrapperfn of unspecified type that has the following members:

      std::mem_fnreturn type

      Member types

      type definition
      result_type(deprecated in C++17) the return type ofpm ifpm is a pointer to member function, not defined for pointer to member object
      argument_type(deprecated in C++17)T*, possibly cv-qualified, ifpm is a pointer to member function taking no arguments
      first_argument_type(deprecated in C++17)T* ifpm is a pointer to member function taking one argument
      second_argument_type(deprecated in C++17)T1 ifpm is a pointer to member function taking one argument of typeT1
      (until C++20)

      Member function

      template<class...Args>

      /* see below */ operator()(Args&&...args)/* cvref-qualifiers */

         noexcept(/* see below */);
      (constexpr since C++20)

      The expressionfn(args) is equivalent toINVOKE(pmd, args), wherepmd is theCallable object held byfn, it is of typeM T::* and is direct-non-list-initialized withpm.

      Thus, the return type ofoperator() isstd::result_of<decltype(pm)(Args&&...)>::typeor equivalentlystd::invoke_result_t<decltype(pm), Args&&...>, and the value innoexcept specifier is equal tostd::is_nothrow_invocable_v<decltype(pm), Args&&...>)(since C++17).

      Each argument inargs is perfectly forwarded, as if bystd::forward<Args>(args)....

      [edit]Example

      Usestd::mem_fn to store and execute a member function and a member object:

      Run this code
      #include <functional>#include <iostream>#include <memory> struct Foo{void display_greeting(){std::cout<<"Hello, world.\n";} void display_number(int i){std::cout<<"number: "<< i<<'\n';} int add_xy(int x,int y){return data+ x+ y;} template<typename...Args>int add_many(Args...args){return data+(args+ ...);} auto add_them(auto...args)// C++20 required{return data+(args+ ...);} int data=7;}; int main(){auto f= Foo{}; auto greet= std::mem_fn(&Foo::display_greeting);    greet(f); auto print_num= std::mem_fn(&Foo::display_number);    print_num(f,42); auto access_data= std::mem_fn(&Foo::data);std::cout<<"data: "<< access_data(f)<<'\n'; auto add_xy= std::mem_fn(&Foo::add_xy);std::cout<<"add_xy: "<< add_xy(f,1,2)<<'\n'; auto u=std::make_unique<Foo>();std::cout<<"access_data(u): "<< access_data(u)<<'\n';std::cout<<"add_xy(u, 1, 2): "<< add_xy(u,1,2)<<'\n'; auto add_many= std::mem_fn(&Foo::add_many<short,int,long>);std::cout<<"add_many(u, ...): "<< add_many(u,1,2,3)<<'\n'; auto add_them= std::mem_fn(&Foo::add_them<short,int,float,double>);std::cout<<"add_them(u, ...): "<< add_them(u,5,7,10.0f,13.0)<<'\n';}

      Output:

      Hello, world.number: 42data: 7add_xy: 10access_data(u): 7add_xy(u, 1, 2): 10add_many(u, ...): 13add_them(u, ...): 42

      [edit] Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2048C++11unnecessary overloads providedremoved
      LWG 2489C++11noexcept not requiredrequired

      [edit]See also

      (C++11)
      copyable wrapper of any copy constructible callable object
      (class template)[edit]
      move-only wrapper of any callable object that supports qualifiers in a given call signature
      (class template)[edit]
      (C++11)
      binds one or more arguments to a function object
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/mem_fn&oldid=173613"

      [8]ページ先頭

      ©2009-2025 Movatter.jp