std::function<R(Args...)>:: operator=
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
|
function::operator=
|
||||
|
(until C++17)
|
||||
| Non-member functions | ||||
|
(until C++20)
|
||||
| Helper classes | ||||
|
(until C++17)
|
||||
| Deduction guides (C++17) |
|
function
&
operator
=
(
const
function
&
other
)
;
|
(1) | (desde C++11) |
|
function
&
operator
=
(
function
&&
other
)
;
|
(2) | (desde C++11) |
|
function
&
operator
=
(
std::
nullptr_t
)
noexcept
;
|
(3) | (desde C++11) |
|
template
<
class
F
>
function & operator = ( F && f ) ; |
(4) | (desde C++11) |
|
template
<
class
F
>
function & operator = ( std:: reference_wrapper < F > f ) noexcept ; |
(5) | (desde C++11) |
Asigna un nuevo
target
a
std::function
.
Args...
y tipo de retorno
R
.
Contenidos |
Parámetros
| otro | - |
otro objeto
std::function
para copiar el destino de
|
| f | - | un invocable para inicializar el target con |
| Requisitos de tipo | ||
-
F
debe cumplir con los requisitos de
Callable
.
|
||
Valor de retorno
* this
Notas
Incluso antes de que se eliminara el soporte de allocator de
std::function
en C++17, estos operadores de asignación utilizan el allocator por defecto en lugar del allocator de
*
this
o del allocator de
other
(consulte
LWG issue 2386
).
Ejemplo
#include <cassert> #include <functional> #include <utility> int inc(int n) { return n + 1; } int main() { std::function<int(int)> f1; std::function<int(int)> f2(inc); assert(f1 == nullptr and f2 != nullptr); f1 = f2; // sobrecarga (1) assert(f1 != nullptr and f1(1) == 2); f1 = std::move(f2); // sobrecarga (2) assert(f1 != nullptr and f1(1) == 2); // f2 está en un estado válido pero no especificado f1 = nullptr; // sobrecarga (3) assert(f1 == nullptr); f1 = inc; // sobrecarga (4) assert(f1 != nullptr and f1(1) == 2); f1 = [](int n) { return n + n; }; // sobrecarga (4) assert(f1 != nullptr and f1(2) == 4); std::reference_wrapper<int(int)> ref1 = std::ref(inc); f1 = ref1; // sobrecarga (5) assert(f1 != nullptr and f1(1) == 2); }
Informes de defectos
Los siguientes informes de defectos que modifican el comportamiento se aplicaron retroactivamente a los estándares de C++ publicados anteriormente.
| DR | Aplicado a | Comportamiento publicado | Comportamiento correcto |
|---|---|---|---|
| LWG 2132 | C++11 | la sobrecarga ( 4 ) que toma un objeto Callable podría ser ambigua | restringida |
| LWG 2401 | C++11 |
el operador de asignación
(
3
)
desde
std::nullptr_t
no requería ser noexcept
|
requerido |
Véase también
|
reemplaza o destruye el objetivo
(función miembro pública de
std::move_only_function
)
|
|
|
(eliminada en C++17)
|
asigna un nuevo objetivo
(función miembro pública) |