(\"" << str << "\", " << x << ")\n"; } int main() { std::function<void(const char*, int)> f1{foo}; std::function<void(const char*, int)> f2{bar}; f1("f1", 1); f2("f2", 2); std::cout << "std::swap(f1, f2);\n"; std::swap(f1, f2); f1("f1", ...
我们不需要考虑被封装目标的具体类型,只需要知道它的调用签名即可。同时,std::function提供了完善的接口,如operator(),swap(),target(),target_type()等,使得我们能够更方便地管理和操作std::function对象。 好的,接着上一段内容,我们以一个具体的例子来展示std::function的使用: 3. std::function的使用示例 让...
)> &lhs, function<R(Args...)> &rhs ) noexcept; (C++17 起) 为std::function 特化std::swap 算法。交换 lhs 与rhs 的状态。等效地调用 lhs.swap(rhs)。 参数 lhs, rhs - 要交换状态的多态函数封装器 返回值 (无) 示例 本节未完成原因:暂无示例 参阅 swap 交换内容 (公开成员函数) ...
// function modifiers: voidswap(function&)_NOEXCEPT; #if_LIBCPP_STD_VER <= 14 template<class_Fp,class_Alloc> _LIBCPP_INLINE_VISIBILITY voidassign(_Fp&& __f,const_Alloc& __a) {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*...
std::swap(std::function) (C++11) specializes thestd::swapalgorithm (function template) operator==operator!= (removed in C++20) compares astd::functionwithnullptr (function template) Helper classes std::uses_allocator<std::function> (C++11)(until C++17) ...
还有一些接口与大部分STL设施相似,有Rule of Five规定的5个方法、swap(),以及std::swap()的特化等。可别小看这个swap(),它有大用处。 总之,函数对象的复制、移动、赋值、交换等操作都是需要的。对客户来说,除了两个std::function的相等性判定(笔者最近在尝试实现这个)以外,其他能想到的方法它都有。
std::swap(std::function) (C++11) 特化std::swap算法 (函数模板) operator==operator!= (C++20 移除) 比较std::function和nullptr (函数模板) 辅助类 std::uses_allocator<std::function> (C++11)(C++17 前) 特化std::uses_allocator类型特征
{function(std::move(__x)).swap(*this);return*this; } function&operator=(nullptr_t) {if(_M_manager) { _M_manager(_M_functor, _M_functor, __destroy_functor); _M_manager =0; _M_invoker =0; }return*this; }template<typename_Functor> ...
(_STD forward<_Fx>(_Val))); } } void _Tidy() noexcept { if (!_Empty()) { // destroy callable object and maybe delete it _Getimpl()->_Delete_this(!_Local()); _Set(nullptr); } } void _Swap(_Func_class& _Right) noexcept { // swap contents with contents of _Right if (...
std::swap(std::function) (C++11) 特化std::swap算法 (函数模板) operator==operator!= (C++20 中移除) 比较std::function和nullptr (函数模板) 辅助类 std::uses_allocator<std::function> (C++11)(C++17 前) 特化std::uses_allocator类型特性 ...