Пространства имён
Варианты
Действия

std::reference_wrapper

Материал из cppreference.com

 
 
 
Функция объектов
Функция обертки
Original:
Function wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
function(C++11)
mem_fn(C++11)
bad_function_call(C++11)
Привязка
Original:
Bind
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
bind(C++11)
is_bind_expression(C++11)
is_placeholder(C++11)
_1, _2, _3, ...(C++11)
Ссылка обертки
Original:
Reference wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reference_wrapper(C++11)
ref
cref
(C++11)
(C++11)
Оператор обертки
Original:
Operator wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Negators
Original:
Negators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Устаревшие связующих и адаптеры
Original:
Deprecated binders and adaptors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_function(устарело)
binary_function(устарело)
ptr_fun(устарело)
pointer_to_unary_function(устарело)
pointer_to_binary_function(устарело)
mem_fun(устарело)
mem_fun_t
mem_fun1_t
const_mem_fun_t
const_mem_fun1_t
(устарело)
(устарело)
(устарело)
(устарело)
mem_fun_ref(устарело)
mem_fun_ref_t
mem_fun1_ref_t
const_mem_fun_ref_t
const_mem_fun1_ref_t
(устарело)
(устарело)
(устарело)
(устарело)
binder1st
binder2nd
(устарело)
(устарело)
bind1st
bind2nd
(устарело)
(устарело)
 
std::reference_wrapper
Член функций
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reference_wrapper::reference_wrapper
reference_wrapper::operator=
reference_wrapper::get
reference_wrapper::operator T&
reference_wrapper::operator()
 
Заголовочный файл <functional>
template< class T >
class reference_wrapper;
(начиная с C++11)
std::reference_wrapper класса шаблона CopyConstructible и CopyAssignable оберткой ссылку на объект или ссылку на функцию типа T. Экземпляры std::reference_wrapper являются объектами (могут быть скопированы или храниться в контейнерах), но они неявно преобразуются в T&, так что они могут быть использованы в качестве аргументов функций, которые принимают базового типа по ссылке.
Original:
Class template std::reference_wrapper is a CopyConstructible and CopyAssignable wrapper around a reference to object or reference to function of type T. Instances of std::reference_wrapper are objects (can be copied or stored in containers), but they are implicitly convertible to T&, so that they can be used as arguments with the functions that take the underlying type by reference.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Вспомогательные функции std::ref и std::cref часто используются для создания объектов std::reference_wrapper.
Original:
Helper functions std::ref and std::cref are often used to generate std::reference_wrapper objects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::reference_wrapper также используется для передачи объектов в std::bind или конструктор std::thread по ссылке.
Original:
std::reference_wrapper is also used to pass objects to std::bind or to the constructor of std::thread by reference.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Содержание

[править] Член типов

типа
Original:
type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
definition
type T
result_type
Тип возвращаемого T если T является функцией. В противном случае, не определен
Original:
The return type of T if T is a function. Otherwise, not defined
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
argument_type
1) если T является функцией или указателем на функцию, которая принимает один аргумент типа A1, то argument_type является A1.

2) если T является указателем на функцию-член класса T0, который не принимает аргументов, то argument_type является T0*, возможно, CV-qualified

3) если T это класс с T::argument_type член типа, то argument_type является псевдонимом этого
Original:
1) if T is a function or pointer to function that takes one argument of type A1, then argument_type is A1.

2) if T is a pointer to member function of class T0 that takes no arguments, then argument_type is T0*, possibly cv-qualified

3) if T is a class type with a member type T::argument_type, then argument_type is an alias of that
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
first_argument_type
1) если T является функцией или указателем на функцию, которая принимает два аргумента типа с A1 и A2, то first_argument_type является A1.

2) если T является указателем на функцию-член класса T0, которая принимает один аргумент, то first_argument_type является T0*, возможно, CV-qualified
3) если T это класс с T::first_argument_type член типа, то first_argument_type является псевдонимом этого

Original:
1) if T is a function or pointer to function that takes two arguments of type s A1 and A2, then first_argument_type is A1.

2) if T is a pointer to member function of class T0 that takes one argument, then first_argument_type is T0*, possibly cv-qualified
3) if T is a class type with a member type T::first_argument_type, then first_argument_type is an alias of that

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
second_argument_type
1) если T является функцией или указателем на функцию, которая принимает два аргумента типа с A1 и A2, то second_argument_type является A2.

2) если T является указателем на функцию-член класса T0, которая принимает один аргумент A1, то second_argument_type является A1, возможно, CV-qualified

3) если T это класс с T::second_argument_type член типа, то first_argument_type является псевдонимом этого
Original:
1) if T is a function or pointer to function that takes two arguments of type s A1 and A2, then second_argument_type is A2.

2) if T is a pointer to member function of class T0 that takes one argument A1, then second_argument_type is A1, possibly cv-qualified

3) if T is a class type with a member type T::second_argument_type, then first_argument_type is an alias of that
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[править] Член функций

содержит ссылку на новый объект std::reference_wrapper
Original:
stores a reference in a new std::reference_wrapper object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(public функция-член) [edit]
повторную привязку std::reference_wrapper
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(public функция-член) [edit]
доступ к сохраненной ссылке
Original:
accesses the stored reference
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(public функция-член) [edit]
вызывает хранимую функцию
Original:
calls the stored function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(public функция-член) [edit]

[править] Пример

Демонстрирует использование reference_wrapper в качестве контейнера ссылок, которые позволяют получить доступ к той же контейнере с помощью нескольких индексов
Original:
Demonstrates the use of reference_wrapper as a container of references, which makes it possible to access the same container using multiple indexes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <functional>
 
int main()
{
    std::list<int> l = {-4, -3, -2, -1, 0, 1, 2, 3, 4};
 
    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
    std::random_shuffle(v.begin(), v.end());
 
    std::vector<std::reference_wrapper<int>> v2(v.begin(), v.end());
    std::partition(v2.begin(), v2.end(), [](int n){return n<0;});
 
    std::cout << "Contents of the list: ";
    for(int n: l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
 
    std::cout << "Contents of the list, shuffled: ";
    for(int i: v) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
 
    std::cout << "Shuffled elements, partitioned: ";
    for(int i: v2) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
}

Вывод:

Contents of the list: -4 -3 -2 -1 0 1 2 3 4
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2
Shuffled elements, partitioned: -3 -1 -2 -4 4 1 3 0 2

[править] См. также

(C++11)
(C++11)
создает std::reference_wrapper с типом выводится из своих аргументов
Original:
creates a std::reference_wrapper with a type deduced from its argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(шаблон функции) [edit]
(C++11)
связывает одну или несколько аргументов функции объекта
Original:
binds one or more arguments to a function object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(шаблон функции) [edit]