Muokkaa

Jaa


reference_wrapper Class

Wraps a reference.

Syntax

template <class Ty>
class reference_wrapper
{
    typedef Ty type;

    reference_wrapper(Ty&) noexcept;
    operator Ty&() const noexcept;
    Ty& get() const noexcept;

    template <class... Types>
    auto operator()(Types&&... args) const ->
        decltype(std::invoke(get(), std::forward<Types>(args)...));
};

Remarks

A reference_wrapper<Ty> is a copy constructible and copy assignable wrapper around a reference to an object or a function of type Ty, and holds a pointer that points to an object of that type. A reference_wrapper can be used to store references in standard containers, and to pass objects by reference to std::bind.

The type Ty must be an object type or a function type, or a static assert fails at compile time.

The helper functions std::ref and std::cref can be used to create reference_wrapper objects.

Members

Constructors

Name Description
reference_wrapper Constructs a reference_wrapper.

Typedefs

Name Description
result_type The weak result type of the wrapped reference.
type The type of the wrapped reference.

Functions

Name Description
get Obtains the wrapped reference.

Operators

Name Description
operator Ty& Gets a pointer to the wrapped reference.
operator() Calls the wrapped reference.

get

Obtains the wrapped reference.

Ty& get() const noexcept;

Remarks

The member function returns the wrapped reference.

Example

// std__functional__reference_wrapper_get.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int main() {
    int i = 1;
    std::reference_wrapper<int> rwi(i);

    std::cout << "i = " << i << std::endl;
    std::cout << "rwi = " << rwi << std::endl;
    rwi.get() = -1;
    std::cout << "i = " << i << std::endl;

    return (0);
}
i = 1
rwi = 1
i = -1

operator Ty&

Gets the wrapped reference.

operator Ty&() const noexcept;

Remarks

The member operator returns *ptr.

Example

// std__functional__reference_wrapper_operator_cast.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int main() {
    int i = 1;
    std::reference_wrapper<int> rwi(i);

    std::cout << "i = " << i << std::endl;
    std::cout << "(int)rwi = " << (int)rwi << std::endl;

    return (0);
}
i = 1
(int)rwi = 1

operator()

Calls the wrapped reference.

template <class... Types>
auto operator()(Types&&... args);

Parameters

Types
The argument list types.

args
The argument list.

Remarks

The template member operator() returns std::invoke(get(), std::forward<Types>(args)...).

Example

// std__functional__reference_wrapper_operator_call.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    std::reference_wrapper<int (int)> rwi(neg);

    std::cout << "rwi(3) = " << rwi(3) << std::endl;

    return (0);
}
rwi(3) = -3

reference_wrapper

Constructs a reference_wrapper.

reference_wrapper(Ty& val) noexcept;

Parameters

Ty
The type to wrap.

val
The value to wrap.

Remarks

The constructor sets the stored value ptr to &val.

Example

// std__functional__reference_wrapper_reference_wrapper.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    int i = 1;
    std::reference_wrapper<int> rwi(i);

    std::cout << "i = " << i << std::endl;
    std::cout << "rwi = " << rwi << std::endl;
    rwi.get() = -1;
    std::cout << "i = " << i << std::endl;

    return (0);
}
i = 1
rwi = 1
i = -1

result_type

The weak result type of the wrapped reference.

typedef R result_type;

Remarks

The result_type typedef is a synonym for the weak result type of a wrapped function. This typedef is only meaningful for function types.

Example

// std__functional__reference_wrapper_result_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    typedef std::reference_wrapper<int (int)> Mywrapper;
    Mywrapper rwi(neg);
    Mywrapper::result_type val = rwi(3);

    std::cout << "val = " << val << std::endl;

    return (0);
}
val = -3

type

The type of the wrapped reference.

typedef Ty type;

Remarks

The typedef is a synonym for the template argument Ty.

Example

// std__functional__reference_wrapper_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    int i = 1;
    typedef std::reference_wrapper<int> Mywrapper;
    Mywrapper rwi(i);
    Mywrapper::type val = rwi.get();

    std::cout << "i = " << i << std::endl;
    std::cout << "rwi = " << val << std::endl;

    return (0);
}
i = 1
rwi = 1