Namespaces
Variants
Views
Actions

std::reference_wrapper::operator()

From cppreference.com
< cpp‎ | utility‎ | functional‎ | reference wrapper
Revision as of 10:54, 30 November 2012 by Cubbi (Talk | contribs)

 
 
 
Function objects
Function wrappers
(C++11)
(C++11)
Bind
(C++11)
Reference wrappers
(C++11)(C++11)
Operator wrappers
Negators
Deprecated 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++17)(until C++17)(until C++17)(until C++17)
(until C++17)(until C++17)
(until C++17)(until C++17)
 
 
template< class... ArgTypes >

typename std::result_of<T&(ArgTypes&&...)>::type

    operator() ( ArgTypes&&... args ) const;
(since C++11)

Calls the callable object, reference to which is stored. This function is available only if the stored reference points to a Callable object.

Contents

Parameters

args - arguments to pass to the called function

Return value

The return value of the called function.

Exceptions

(none)

Example

#include <iostream>
#include <functional>
 
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 called
bind expression called with 7 as the argument
lambda function called

See also

accesses the stored reference
(public member function) [edit]