BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsl::function< PROTOTYPE > Class Template Reference

#include <bslstl_function.h>

Inheritance diagram for bsl::function< PROTOTYPE >:

Public Types

typedef Function_Rep::allocator_type allocator_type
 

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (function, BloombergLP::bslma::UsesBslmaAllocator)
 
 BSLMF_NESTED_TRAIT_DECLARATION (function, BloombergLP::bslmf::UsesAllocatorArgT)
 
 BSLMF_NESTED_TRAIT_DECLARATION (function, bsl::is_nothrow_move_constructible)
 
 function () BSLS_KEYWORD_NOEXCEPT
 
 function (nullptr_t) BSLS_KEYWORD_NOEXCEPT
 
 function (allocator_arg_t, const allocator_type &allocator) BSLS_KEYWORD_NOEXCEPT
 
 function (allocator_arg_t, const allocator_type &allocator, nullptr_t) BSLS_KEYWORD_NOEXCEPT
 
template<class FUNC >
 function (BSLS_COMPILERFEATURES_FORWARD_REF(FUNC) func, typename enable_if< ! IsReferenceCompatible< typename Decay< FUNC >::type, function >::value &&IsInvocableWithPrototype< typename Decay< FUNC >::type >::value &&! MovableRefUtil::IsMovableReference< FUNC >::value, int >::type=0)
 
template<class FUNC >
 function (const BloombergLP::bslmf::MovableRef< FUNC > &func, typename enable_if< ! IsReferenceCompatible< typename Decay< FUNC >::type, function >::value &&IsInvocableWithPrototype< typename Decay< FUNC >::type >::value, int >::type=0)
 
template<class FUNC >
 function (allocator_arg_t, const allocator_type &allocator, BSLS_COMPILERFEATURES_FORWARD_REF(FUNC) func, typename enable_if< ! IsReferenceCompatible< typename Decay< FUNC >::type, function >::value &&IsInvocableWithPrototype< typename Decay< FUNC >::type >::value, int >::type=0)
 
 function (const function &original)
 
 function (allocator_arg_t, const allocator_type &allocator, const function &original)
 
 function (BloombergLP::bslmf::MovableRef< function > original) BSLS_KEYWORD_NOEXCEPT
 
 function (allocator_arg_t, const allocator_type &allocator, BloombergLP::bslmf::MovableRef< function > original)
 
functionoperator= (const function &rhs)
 
functionoperator= (BloombergLP::bslmf::MovableRef< function > rhs)
 
template<class FUNC >
enable_if<!IsReferenceCompatible< typenameDecay< FUNC >::type, function >::value &&IsInvocableWithPrototype< typenameDecay< FUNC >::type >::value, function & >::type operator= (BSLS_COMPILERFEATURES_FORWARD_REF(FUNC) rhs)
 
template<class FUNC >
enable_if< IsInvocableWithPrototype< typenameDecay< FUNC >::type >::value, function & >::type operator= (bsl::reference_wrapper< FUNC > rhs) BSLS_KEYWORD_NOEXCEPT
 
functionoperator= (nullptr_t) BSLS_KEYWORD_NOEXCEPT
 Set this object to empty and return *this.
 
void swap (function &other) BSLS_KEYWORD_NOEXCEPT
 
template<class TP >
TP * target () BSLS_KEYWORD_NOEXCEPT
 
 operator UnspecifiedBool () const BSLS_KEYWORD_NOEXCEPT
 
allocator_type get_allocator () const BSLS_KEYWORD_NOEXCEPT
 
template<class TP >
const TP * target () const BSLS_KEYWORD_NOEXCEPT
 
const std::type_info & target_type () const BSLS_KEYWORD_NOEXCEPT
 
 operator BloombergLP::bdef_Function< PROTOTYPE * > & () BSLS_KEYWORD_NOEXCEPT
 
 operator const BloombergLP::bdef_Function< PROTOTYPE * > & () const BSLS_KEYWORD_NOEXCEPT
 
BloombergLP::bslma::Allocator * allocator () const BSLS_KEYWORD_NOEXCEPT
 
bool isInplace () const BSLS_KEYWORD_NOEXCEPT
 

Detailed Description

template<class PROTOTYPE>
class bsl::function< PROTOTYPE >

This class template implements the C++ Standard Library std::function template, enhanced for allocator support as per Standards Proposal P0987. An instantiation of this template generalizes the notion of a pointer to a function having the specified PROTOTYPE expressed as a function type (e.g., int(const char *, float)). An object of this class wraps a copy of the callable object specified at construction (if any), such as a function pointer, member-function pointer, member-data pointer, or functor object. The wrapped object (called the target or target object) is owned by the bsl::function object (unlike the function pointer that it mimics). Invoking the bsl::function object will invoke the target (or throw an exception, if there is no target). Note that function will compile only if PROTOTYPE is a function type.

To optimize away many heap allocations, objects of this type have a buffer into which small callable objects can be stored. In order to qualify for this small-object optimization, a callable type must not only fit in the buffer but must also be nothrow move constructible. The latter constraint allows this type to be nothrow move constructible and nothrow swappable, as required by the C++ Standard. The small object buffer is guaranteed to be large enough to hold a pointer to function, pointer to member function, pointer to member data, a bsl::reference_wrapper, or an empty struct. Although the standard does not specify a minimum size beyond the aforementioned guarantee, many small structs will fit in the small object buffer, as defined in the bslstl_function_smallobjectoptimization component.

See bslstl_function

Member Typedef Documentation

◆ allocator_type

template<class PROTOTYPE >
typedef Function_Rep::allocator_type bsl::function< PROTOTYPE >::allocator_type

Constructor & Destructor Documentation

◆ function() [1/3]

template<class PROTOTYPE >
template<class FUNC >
bsl::function< PROTOTYPE >::function ( BSLS_COMPILERFEATURES_FORWARD_REF(FUNC)  func,
typename enable_if< ! IsReferenceCompatible< typename Decay< FUNC >::type, function< PROTOTYPE > >::value &&IsInvocableWithPrototype< typename Decay< FUNC >::type >::value &&! MovableRefUtil::IsMovableReference< FUNC >::value, int >::type  = 0 
)
inline

Create an object wrapping the specified func callable object. Use the default allocator to supply memory. If func is a null pointer or null pointer-to-member, then the resulting object will be empty. This constructor will not participate in overload resolution if func is of the same type as (or reference compatible with) this object (to avoid ambiguity with the copy and move constructors) or is an integral type (to avoid matching null pointer literals). In C++03, this function will not participate in overload resolution if FUNC is a MovableRef (see overload, below), and instantiation will fail unless FUNC is invocable using the arguments and return type specified in PROTOTYPE. In C++11 and later, this function will not participate in overload resolution if FUNC is not invocable using the arguments and return type specified in PROTOTYPE. Note that this constructor implicitly converts from any type that is so invocable.

Implementation Note


◆ function() [2/3]

template<class PROTOTYPE >
template<class FUNC >
bsl::function< PROTOTYPE >::function ( const BloombergLP::bslmf::MovableRef< FUNC > &  func,
typename enable_if< ! IsReferenceCompatible< typename Decay< FUNC >::type, function< PROTOTYPE > >::value &&IsInvocableWithPrototype< typename Decay< FUNC >::type >::value, int >::type  = 0 
)
inlineexplicit

Create an object wrapping the specified func callable object. This constructor (ctor 2) is identical to the previous constructor (ctor 1) except that, in C++03 ctor 2 provides for explicit construction from a MovableRef referencing a callable type, rather than an implicit conversion for FUNC not being a MovableRef. In C++11, overload resolution matching an argument of type T&& to a parameter of type T (exact match) is always preferred over matching T&& to bsl::function (conversion). In C++03, however MovableRef is not a real reference type, so it sometimes creates overload ambiguities whereby matching MovableRef<T> to T (conversion) is no better than matching MovableRef<T> to bsl::function (also conversion). This ambiguity is resolved by making this constructor from MovableRef<T> explicit, while leaving other constructor from FUNC implicit. This means that move will fail in a narrow set of cases in C++03, as shown below:

typedef bsl::function<void(int)> Obj;
MyCallableType x;
Obj f1 = x; // OK
Obj f2 = bslmf::MovableRefUtil::move(x); // No conversion in C++03
Obj f3(bslmf::MovableRefUtil::move(x)); // OK, normal ctor call
void y(const Obj& f);
y(x); // OK
y(bslmf::MovableRefUtil::move(x)); // Not found in C++03
y(Obj(bslmf::MovableRefUtil::move(x))); // OK, explicit cast
Forward declaration.
Definition bslstl_function.h:934
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1060

As you can see from the examples above, there are simple workarounds for the problem cases, although generic code might need to be extra careful.

Implementation Note


◆ function() [3/3]

template<class PROTOTYPE >
template<class FUNC >
bsl::function< PROTOTYPE >::function ( allocator_arg_t  ,
const allocator_type allocator,
BSLS_COMPILERFEATURES_FORWARD_REF(FUNC)  func,
typename enable_if< ! IsReferenceCompatible< typename Decay< FUNC >::type, function< PROTOTYPE > >::value &&IsInvocableWithPrototype< typename Decay< FUNC >::type >::value, int >::type  = 0 
)
inline

Create an object wrapping the specified func callable object. Use the specified allocator (i.e., the address of a bslma::Allocator object) to supply memory. If func is a null pointer or null pointer-to-member, then the resulting object will be empty. This constructor will not participate in overload resolution if func is of the same type as (or reference compatible with) this object (to avoid ambiguity with the extended copy and move constructors) or is an integral type (to avoid matching null pointer literals). In C++03, this function will not participate in overload resolution if FUNC is a MovableRef (see overload, below), and instantiation will fail unless FUNC is invocable using the arguments and return type specified in PROTOTYPE. In C++11 and later, this function will not participate in overload resolution if FUNC is not invocable using the arguments and return type specified in PROTOTYPE. Note that this constructor implicitly converts from any type that is so invocable.

Implementation Note


Member Function Documentation

◆ BSLMF_NESTED_TRAIT_DECLARATION() [1/3]

template<class PROTOTYPE >
bsl::function< PROTOTYPE >::BSLMF_NESTED_TRAIT_DECLARATION ( function< PROTOTYPE >  ,
BloombergLP::bslma::UsesBslmaAllocator   
)

◆ BSLMF_NESTED_TRAIT_DECLARATION() [2/3]

template<class PROTOTYPE >
bsl::function< PROTOTYPE >::BSLMF_NESTED_TRAIT_DECLARATION ( function< PROTOTYPE >  ,
BloombergLP::bslmf::UsesAllocatorArgT   
)

◆ BSLMF_NESTED_TRAIT_DECLARATION() [3/3]

template<class PROTOTYPE >
bsl::function< PROTOTYPE >::BSLMF_NESTED_TRAIT_DECLARATION ( function< PROTOTYPE >  ,
bsl::is_nothrow_move_constructible   
)

◆ operator UnspecifiedBool()

template<class PROTOTYPE >
bsl::function< PROTOTYPE >::operator UnspecifiedBool ( ) const
inline

(C++03 only) Return a null value if this object is empty, otherwise an arbitrary non-null value. Note that this operator will be invoked implicitly in boolean contexts such as in the condition of an if or while statement, but does not constitute an implicit conversion to bool.

◆ operator=() [1/2]

template<class PROTOTYPE >
template<class FUNC >
enable_if< IsInvocableWithPrototype< typenameDecay< FUNC >::type >::value, function & >::type bsl::function< PROTOTYPE >::operator= ( bsl::reference_wrapper< FUNC >  rhs)
inline

Destroy the current target (if any) of this object, then set the target to the specified rhs wrapper containing a reference to a callable object and return *this. The result is equivalent to having constructed *this from rhs and this->get_allocator(). Note that this assignment is a separate overload only because it is unconditionally noexcept.

Implementation Note


◆ operator=() [2/2]

template<class PROTOTYPE >
template<class FUNC >
enable_if<!IsReferenceCompatible< typenameDecay< FUNC >::type, function >::value &&IsInvocableWithPrototype< typenameDecay< FUNC >::type >::value, function & >::type bsl::function< PROTOTYPE >::operator= ( BSLS_COMPILERFEATURES_FORWARD_REF(FUNC)  rhs)
inline

Set the target of this object to the specified rhs callable object, destroy the previous target (if any), and return *this. The result is equivalent to having constructed *this from std::forward<FUNC>(rhs) and this->get_allocator(). Note that this assignment operator will not participate in overload resolution if func is of the same type as this object (to avoid ambiguity with the copy and move assignment operators.) In C++03, instantiation will fail unless FUNC is invocable with the arguments and return type specified in PROTOTYPE. In C++11 and later, this assignment operator will not participate in overload resolution unless FUNC is invocable with the arguments and return type specified in PROTOTYPE.

Implementation Note



The documentation for this class was generated from the following file: