orm*_*orm 51 c++ move-semantics c++11
因为std::function是可复制的,所以标准要求用于构造它的callables也是可复制的:
n337(20.8.11.2.1)
template<class F> function(F f);要求:
F应为CopyConstructible.f对于参数类型ArgTypes和返回类型,应为Callable(20.8.11.2)R.A的拷贝构造函数和析构函数不会抛出异常
这意味着不可能std::function从不可复制的绑定对象或捕获仅移动类型的lambda形成std::unique_ptr.
似乎可以为仅移动的callables实现这样一个仅移动的包装器.是否存在标准库仅限移动等效std::function或者,是否存在针对此问题的常见解决方法?
Yak*_*ont 17
不,std::functionC++ std库中没有只移动版本.(截至C++ 14)
最快的代表是的实现std::function恰好是比大多数更快类一样std::function在许多实现std库,它应该是容易分叉成move和copy版本.
将move唯一的函数对象包装到shared_ptr<F>具有转发的类中operator()是另一种方法.
这是一个task草图:
template<class Sig>
struct task;
namespace details {
template<class Sig>
struct task_iimpl;
template<class R, class...Args>
struct task_iimpl<R(Args...)> {
virtual ~task_iimpl() {}
virtual R invoke(Args&&...args) const = 0;
};
template<class F, class Sig>
struct task_impl;
template<class F, class R, class...Args>
struct task_impl<F,R(Args...)>:
task_iimpl<R(Args...)>
{
F f;
template<class T>
task_impl(T&& t):f(std::forward<T>(t)) {}
virtual R invoke(Args&&...args) const override {
return f( std::forward<Args>(args...) );
}
};
template<class F, class...Args>
struct task_impl<F,void(Args...)>:
task_iimpl<void(Args...)>
{
F f;
template<class T>
task_impl(T&& t):f(std::forward<T>(t)) {}
virtual void invoke(Args&&...args) const override {
f( std::forward<Args>(args...) );
}
};
}
template<class R, class...Args>
struct task<R(Args...)> {
virtual ~task_iimpl() {}
R operator()(Args...args) const {
return pImpl->invoke(std::forward<Args>(args...));
}
explicit operator bool()const{ return static_cast<bool>(pImpl); }
task(task &&)=default;
task& operator=(task &&)=default;
task()=default;
// and now for a mess of constructors
// the rule is that a task can be constructed from anything
// callable<R(Args...)>, destroyable, and can be constructed
// from whatever is passed in. The callable feature is tested for
// in addition, if constructed from something convertible to `bool`,
// then if that test fails we construct an empty task. This makes us work
// well with empty std::functions and function pointers and other tasks
// that are call-compatible, but not exactly the same:
struct from_func_t {};
template<class F,
class dF=std::decay_t<F>,
class=std::enable_if_t<!std::is_same<dF, task>{}>,
class FR=decltype(std::declval<F const&>()(std::declval<Args>()...)),
std::enable_if_t<std::is_same<R, void>{} || std::is_convertible<FR, R>{} >*=0,
std::enable_if_t<std::is_convertible<dF, bool>{}>*=0
>
task(F&& f):
task(
static_cast<bool>(f)?
task( from_func_t{}, std::forward<F>(f) ):
task()
)
{}
template<class F,
class dF=std::decay_t<F>,
class=std::enable_if_t<!std::is_same<dF, task>{}>,
class FR=decltype(std::declval<F const&>()(std::declval<Args>()...)),
std::enable_if_t<std::is_same<R, void>{} || std::is_convertible<FR, R>{} >*=0,
std::enable_if_t<!std::is_convertible<dF, bool>{}>*=0
>
task(F&& f):
task( from_func_t{}, std::forward<F>(f) )
{}
task(std::nullptr_t):task() {}
// overload resolution helper when signatures match exactly:
task( R(*pf)(Args...) ):
task( pf?task( from_func_t{}, pf ):task() )
{}
private:
template<class F,
class dF=std::decay_t<F>
>
task(from_func_t, F&& f):
pImpl( std::make_unique<details::task_impl<dF,R(Args...)>>(
std::forward<F>(f)
)
{}
std::unique_ptr<details::task_iimpl<R(Args...)> pImpl;
};
Run Code Online (Sandbox Code Playgroud)
但它还没有经过测试或编译,我只是写了它.
更具工业强度的版本将包括一个小缓冲区优化(SBO)来存储小的可调用(假设它们是可移动的;如果不是可移动的,则存储在堆上以允许移动),以及一个get-pointer-if-you-the-the- type-right(like std::function).
msk*_*her 13
是的,在 C++23 的当前草案中有一个关于 std::move_only_function 的提案,于 2021-10 采纳:
本文提出了一个保守的、仅移动的等价物
std::function。
另请参阅cppreference 条目std::move_only_function:
类模板
std::move_only_function是一个通用的多态函数包装器。std::move_only_function对象可以存储和调用任何可构造的(不需要移动构造的)可调用目标——函数、lambda 表达式、绑定表达式或其他函数对象,以及指向成员函数的指针和指向成员对象的指针。
...满足MoveConstructible和MoveAssignable
std::move_only_function的要求,但不满足CopyConstructible或CopyAssignable。
正如其他人指出的那样,std::function库中没有仅移动版本。以下是重用(滥用?)std::function并允许它接受仅移动类型的解决方法。它很大程度上受到了评论中dyp 实现的启发,因此很多功劳归功于他:
#include <functional>
#include <iostream>
#include <type_traits>
#include <utility>
template<typename T>
class unique_function : public std::function<T>
{
template<typename Fn, typename En = void>
struct wrapper;
// specialization for CopyConstructible Fn
template<typename Fn>
struct wrapper<Fn, std::enable_if_t< std::is_copy_constructible<Fn>::value >>
{
Fn fn;
template<typename... Args>
auto operator()(Args&&... args) { return fn(std::forward<Args>(args)...); }
};
// specialization for MoveConstructible-only Fn
template<typename Fn>
struct wrapper<Fn, std::enable_if_t< !std::is_copy_constructible<Fn>::value
&& std::is_move_constructible<Fn>::value >>
{
Fn fn;
wrapper(Fn&& fn) : fn(std::forward<Fn>(fn)) { }
wrapper(wrapper&&) = default;
wrapper& operator=(wrapper&&) = default;
// these two functions are instantiated by std::function
// and are never called
wrapper(const wrapper& rhs) : fn(const_cast<Fn&&>(rhs.fn)) { throw 0; } // hack to initialize fn for non-DefaultContructible types
wrapper& operator=(wrapper&) { throw 0; }
template<typename... Args>
auto operator()(Args&&... args) { return fn(std::forward<Args>(args)...); }
};
using base = std::function<T>;
public:
unique_function() noexcept = default;
unique_function(std::nullptr_t) noexcept : base(nullptr) { }
template<typename Fn>
unique_function(Fn&& f) : base(wrapper<Fn>{ std::forward<Fn>(f) }) { }
unique_function(unique_function&&) = default;
unique_function& operator=(unique_function&&) = default;
unique_function& operator=(std::nullptr_t) { base::operator=(nullptr); return *this; }
template<typename Fn>
unique_function& operator=(Fn&& f)
{ base::operator=(wrapper<Fn>{ std::forward<Fn>(f) }); return *this; }
using base::operator();
};
using std::cout; using std::endl;
struct move_only
{
move_only(std::size_t) { }
move_only(move_only&&) = default;
move_only& operator=(move_only&&) = default;
move_only(move_only const&) = delete;
move_only& operator=(move_only const&) = delete;
void operator()() { cout << "move_only" << endl; }
};
int main()
{
using fn = unique_function<void()>;
fn f0;
fn f1 { nullptr };
fn f2 { [](){ cout << "f2" << endl; } }; f2();
fn f3 { move_only(42) }; f3();
fn f4 { std::move(f2) }; f4();
f0 = std::move(f3); f0();
f0 = nullptr;
f2 = [](){ cout << "new f2" << endl; }; f2();
f3 = move_only(69); f3();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6242 次 |
| 最近记录: |