Namespaces
Variants

std:: move_only_function

From cppreference.net
Utilities library
Function objects
Function wrappers
(C++11)
move_only_function
(C++23)
(C++11)
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++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* )
定义于头文件 <functional>
template < class ... >
class move_only_function ; // 未定义
(1) (C++23 起)
template < class R, class ... Args >

class move_only_function < R ( Args... ) > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) noexcept > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) & > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) & noexcept > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) && > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) && noexcept > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) const > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) const noexcept > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) const & > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) const & noexcept > ;
template < class R, class ... Args >
class move_only_function < R ( Args... ) const && > ;
template < class R, class ... Args >

class move_only_function < R ( Args... ) const && noexcept > ;
(2) (C++23 起)

类模板 std::move_only_function 是一种通用多态函数包装器。 std::move_only_function 对象可以存储并调用任何可构造(不要求可移动构造)的 可调用 目标 ——包括函数、 lambda 表达式 绑定表达式 或其他函数对象,以及指向成员函数的指针和指向成员对象的指针。

存储的可调用对象称为 std::move_only_function 目标 。若 std::move_only_function 不包含目标,则称其为 。与 std::function 不同,调用 std::move_only_function 会导致未定义行为。

std::move_only_function 支持其模板参数中提供的所有可能的 cv限定符 (不包括 volatile )、 引用限定符 noexcept说明符 的组合。这些限定符和说明符(如果存在)会被添加到其 operator() 中。

std::move_only_function 满足 MoveConstructible MoveAssignable 的要求,但不满足 CopyConstructible CopyAssignable 的要求。

目录

成员类型

类型 定义
result_type R

成员函数

构造新的 std::move_only_function 对象
(公开成员函数)
销毁 std::move_only_function 对象
(公开成员函数)
替换或销毁目标对象
(公开成员函数)
交换两个 std::move_only_function 对象的目标
(公开成员函数)
检查 std::move_only_function 是否拥有目标对象
(公开成员函数)
调用目标对象
(公开成员函数)

非成员函数

特化 std::swap 算法
(函数)
(C++23)
比较 std::move_only_function nullptr
(函数)

注释

实现可以在 std::move_only_function 对象内部存储小型可调用对象。这种小型对象优化对于函数指针和 std::reference_wrapper 特化实际上是必需的,并且仅适用于满足 std:: is_nothrow_move_constructible_v < T > true 的类型 T

如果返回引用的 std::move_only_function 由返回纯右值的函数或函数对象(包括无尾随返回类型的 lambda 表达式)初始化,程序将处于非良构状态,因为禁止将返回的引用绑定到临时对象。另请参阅 std::function 说明。

功能测试 标准 功能
__cpp_lib_move_only_function 202110L (C++23) std::move_only_function

示例

#include <functional>
#include <future>
#include <iostream>
int main()
{
    std::packaged_task<double()> packaged_task([](){ return 3.14159; });
    std::future<double> future = packaged_task.get_future();
    auto lambda = [task = std::move(packaged_task)]() mutable { task(); };
//  std::function<void()> function = std::move(lambda); // 错误
    std::move_only_function<void()> function = std::move(lambda); // 正确
    function();
    std::cout << future.get();
}

输出:

3.14159

参见

(C++11)
任何可复制构造的可调用对象的可复制包装器
(类模板)
任何可调用对象的非占有包装器
(类模板)
支持给定调用签名中限定符的任何可复制构造可调用对象的可复制包装器
(类模板)