c++ - Boost.TypeErasure: movable functor
问题描述
Since std::function
requires copy semantics and captured lambda cannot be converted to std::function
, I try to define movable function using boost.TypeErasure. Everything is OK until move assign operator is required.
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/constructible.hpp>
#include <boost/type_erasure/callable.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/mpl/vector.hpp>
namespace bte = boost::type_erasure;
namespace bm = boost::mpl;
using Foo = bte::any<
bm::vector<
bte::constructible<bte::_self(bte::_self&&)>,
bte::assignable<bte::_self, bte::_self&&>,
bte::destructible<>,
bte::callable<void()>
>
>;
int main()
{
Foo{[&]{}};
}
With gcc, the compile error is:
In file included from /usr/local/include/boost/type_erasure/detail/normalize.hpp:34,
from /usr/local/include/boost/type_erasure/any.hpp:36,
from main.cpp:1:
/usr/local/include/boost/type_erasure/builtin.hpp: In instantiation of 'static void boost::type_erasure::assignable<T, U>::apply(T&, const U&) [with T = main()::<lambda()>; U = main()::<lambda()>&&]':
/usr/local/include/boost/type_erasure/detail/instantiate.hpp:91:9: required from 'static void boost::type_erasure::detail::instantiate_concept4::apply(Concept*, Map*) [with Concept = boost::mpl::vector<boost::type_erasure::constructible<boost::type_erasure::_self(boost::type_erasure::_self&&)>, boost::type_erasure::assignable<boost::type_erasure::_self, boost::type_erasure::_self&&>, boost::type_erasure::destructible<>, boost::type_erasure::callable<void()> >; Map = boost::mpl::map1<boost::mpl::pair<boost::type_erasure::_self, main()::<lambda()> > >]'
/usr/local/include/boost/type_erasure/any.hpp:225:13: required from 'boost::type_erasure::any<Concept, T>::any(U&&) [with U = main()::<lambda()>; Concept = boost::mpl::vector<boost::type_erasure::constructible<boost::type_erasure::_self(boost::type_erasure::_self&&)>, boost::type_erasure::assignable<boost::type_erasure::_self, boost::type_erasure::_self&&>, boost::type_erasure::destructible<>, boost::type_erasure::callable<void()> >; T = boost::type_erasure::_self]'
main.cpp:21:14: required from here
/usr/local/include/boost/type_erasure/builtin.hpp:73:51: error: use of deleted function 'main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&)'
static void apply(T& dst, const U& src) { dst = src; }
~~~~^~~~~
main.cpp:21:11: note: a lambda closure type has a deleted copy assignment operator
Foo{[&]{}};
^
I don't understand why apply
's argument is const
. And what is the correct way?
Update:
I thought lambda is move-assignable. However, it seems wrong.
解决方案
推荐阅读
- java - 删除注释时无法维护代码结构
- android - 如何使图像按钮上的位图均匀拉伸
- sql - 我需要一个查询来从 jpa 存储库中获取结果
- javascript - 什么是“连接”:函数()在此代码段中的含义
- android - 在视图模型中使用 mediatorlivedata 对事件进行排序时如何避免回调地狱
- javascript - 通过 Selenium 在 Python 中运行带有变量的 execute_script
- iis - 如何修复从 https//www 到 https//non-www 版本的网站的重定向?
- java - java编译错误:方法内部的方法
- javascript - 为什么连接和排序方法表现得很奇怪?2 本质上相同的方式不会以相同的方式工作
- python - 绘制连接时间序列中大多数数据点的线