Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::reference_wrapper<T>::operator()

      From cppreference.com
      <cpp‎ |utility‎ |functional‎ |reference wrapper
       
       
      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...ArgTypes>

      typenamestd::result_of<T&(ArgTypes&&...)>::type

          operator()( ArgTypes&&...args)const;
      (since C++11)
      (until C++17)
      template<class...ArgTypes>

      std::invoke_result_t<T&, ArgTypes...>

          operator()( ArgTypes&&...args)constnoexcept(/* see below */);
      (since C++17)
      (constexpr since C++20)

      Calls theCallable object, reference to which is stored, as if byINVOKE(get(),std::forward<ArgTypes>(args)...). This function is available only if the stored reference points to aCallable object.

      T must be a complete type.

      Contents

      [edit]Parameters

      args - arguments to pass to the called function

      [edit]Return value

      The return value of the called function.

      [edit]Exceptions

      May throw implementation-defined exceptions.

      (since C++11)
      (until C++17)
      noexcept specification:  
      noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>)
      (since C++17)

      [edit]Example

      Run this code
      #include <functional>#include <iostream> void f1(){std::cout<<"reference to function called\n";} void f2(int n){std::cout<<"bind expression called with "<< n<<" as the argument\n";} int main(){std::reference_wrapper<void()> ref1=std::ref(f1);    ref1(); auto b=std::bind(f2, std::placeholders::_1);auto ref2=std::ref(b);    ref2(7); auto c=[]{std::cout<<"lambda function called\n";};auto ref3=std::ref(c);    ref3();}

      Output:

      reference to function calledbind expression called with 7 as the argumentlambda function called

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3764C++17operator() is notnoexceptpropagatenoexcept

      [edit]See also

      accesses the stored reference
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/reference_wrapper/operator()&oldid=171006"

      [8]ページ先頭

      ©2009-2025 Movatter.jp