Namespaces
Variants

std::experimental::function<R(Args...)>:: function

From cppreference.net
function ( ) noexcept ;
(1) (库基础技术规范)
function ( std:: nullptr_t ) noexcept ;
(2) (库基础技术规范)
function ( const function & other ) ;
(3) (库基础技术规范)
function ( function && other ) ;
(4) (库基础技术规范)
template < class F >
function ( F f ) ;
(5) (库基础技术规范)
(6)
template < class Alloc >
function ( std:: allocator_arg_t , const Alloc & alloc ) noexcept ;
(库基础技术规范)
function ( std:: allocator_arg_t ,
const allocator_type & alloc ) noexcept ;
(库基础技术规范 v3)
(7)
template < class Alloc >

function ( std:: allocator_arg_t , const Alloc & alloc,

std:: nullptr_t ) noexcept ;
(库基础技术规范)
function ( std:: allocator_arg_t , const allocator_type & alloc,
std:: nullptr_t ) noexcept ;
(库基础技术规范 v3)
(8)
template < class Alloc >

function ( std:: allocator_arg_t , const Alloc & alloc,

const function & other ) ;
(库基础技术规范)
function ( std:: allocator_arg_t , const allocator_type & alloc,
const function & other ) ;
(库基础技术规范 v3)
(9)
template < class Alloc >

function ( std:: allocator_arg_t , const Alloc & alloc,

function && other ) ;
(库基础技术规范)
function ( std:: allocator_arg_t , const allocator_type & alloc,
function && other ) ;
(库基础技术规范 v3)
(10)
template < class F, class Alloc >
function ( std:: allocator_arg_t , const Alloc & alloc, F f ) ;
(库基础技术规范)
function ( std:: allocator_arg_t , const allocator_type & alloc, F f ) ;
(库基础技术规范 v3)

从多种来源构造一个 std::experimental::function

1,2) 创建一个 函数。
3) other 目标 复制到 * this 目标 。如果 other ,则调用后 * this 也将变为
4) other 目标对象 移动到 * this 目标对象 。如果 other ,则调用后 * this 也将为 构造完成后, * this 将存储 other. get_allocator ( ) 的副本。 (库基础技术规范 v3)
5) 使用 f 的副本初始化 目标对象 。如果 f 是空函数指针或空成员指针,则调用后 * this 将为 空状态 。除非 f 对于参数类型 Args... 和返回类型 R 满足 Callable 要求,否则此构造函数不参与重载决议。
6-10) (1-5) 相同,区别在于使用 alloc function 可能使用的任何内部数据结构分配内存。 这些构造函数将 alloc 视为类型擦除分配器(参见下文)。 (直至库基础技术规范 v3)

通过 (1-5) 构造后, this - > get_memory_resource ( ) 返回的值与构造期间 std:: experimental :: pmr :: get_default_resource ( ) 的值相同。

(library fundamentals TS)
(until library fundamentals TS v3)

通过 (1-3) (5) 构造后, * this 存储一个默认构造的 std:: pmr :: polymorphic_allocator <>

(library fundamentals TS v3)

目标 是函数指针或 std::reference_wrapper 时,保证进行小对象优化,即这些目标始终直接存储在 std::experimental::function 对象内部,不会发生动态内存分配。其他大型对象可能在动态分配的存储空间中构造,并通过指针由 std::experimental::function 对象进行访问。

如果构造函数移动或复制函数对象(包括 std::experimental::function 的实例),则该移动或复制操作通过 使用分配器构造 执行,使用的分配器为 this - > get_memory_resource ( ) (直至库基础技术规范 v3) this - > get_allocator ( ) (库基础技术规范 v3)

目录

类型擦除分配器

function 的构造函数接受分配器参数 alloc 时,会将该参数视为类型擦除的分配器。 function 用于分配内存的内存资源指针通过以下方式根据分配器参数(若指定)确定:

alloc 的类型 内存资源指针的值
不存在(构造时未指定分配器) 构造时 std:: experimental :: pmr :: get_default_resource ( ) 的值
std::nullptr_t 构造时 std:: experimental :: pmr :: get_default_resource ( ) 的值
可转换为
std:: experimental :: pmr :: memory_resource * 的指针类型
static_cast < std:: experimental :: pmr :: memory_resource * > ( alloc )

std::experimental::pmr::polymorphic_allocator 的特化
alloc. resource ( )
满足 Allocator 要求的任何其他类型 指向类型为 std:: experimental :: pmr :: resource_adaptor < A > ( alloc ) 的值的指针,其中 A alloc 的类型。该指针仅在 function 对象的生命周期内保持有效。
以上皆不符合 程序非良构。

参数

other - 用于初始化 * this 的函数对象
f - 用于初始化 * this 的可调用对象
alloc - 用于内部内存分配的分配器
类型要求
-
F 必须满足 Callable CopyConstructible 的要求。

异常

3,8) other target 为函数指针或 std::reference_wrapper 时不抛出异常,否则可能抛出 std::bad_alloc 或存储的可调用对象复制构造函数所抛出的任何异常。
4) (无)
5,10) f 为函数指针或 std::reference_wrapper 则不抛出异常,否则可能抛出 std::bad_alloc 或存储的可调用对象拷贝构造函数所抛出的任何异常。
9) (无)

示例