问题标签 [boost-move]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
480 浏览

c++ - 在 C++03 中将 std::string 移动到 boost::thread 中

我在没有 C++11 的多个平台和编译器上使用 boost 1.51。

在我的主线程中,我有一个非常长且昂贵的复制,std::string veryLongString我需要将其传递给一个新线程进行处理。
创建新线程后,我不再使用veryLongString主线程,所以我想它移到boost::threadctor中。
主线程或 的范围veryLongString可能在新线程完成之前结束,因此通过引用传递(例如 with boost::ref)不是一种选择。

显然,如果veryLongString创建为 ashared_ptr<std::string>那么我可以将其复制shared_ptr到线程 ctor 中,但事实并非如此,所以无论如何我都需要复制它。

我怎样才能[boost::]move() veryLongString进入boost::threadctor (可能使用 via boost::bind)?这可能吗?

0 投票
2 回答
1798 浏览

c++ - 我应该如何分配从工厂函数返回的 boost::interprocess::unique_ptr,可能在 C++03 中使用 boost::move

我正在尝试创建一个返回 boost::interprocess::unique_ptr 的工厂函数。这是一个例子:

问题是 gcc 无法编译上面的代码:

当我改变

然后 gcc 说:

难道我做错了什么?

有趣的是,当我这样做时:

代码编译没有任何问题。

顺便说一句:我使用 gcc v4.4.3 和 Boost v1.51.0。


更新:

通过使用以下代码段,我已经能够克服这个问题:

上述演员表基于operator=原始问题中提到的模糊重载的第一个版本。第二个 ( operator=(int boost::interprocess::unique_ptr<T, D>::nat::*) 可能是由实现模拟提供的std::unique_ptr::operator=(nullptr_t),事实上它重置了unique_ptr. 事实证明,这也让人operator=模棱两可。

不幸的是,使用上述static_cast<>()方法使使用我的工厂变得过于复杂。

解决此问题的一种方法是删除第二个重载 for operator=,因为人们总是可以显式调用unique_ptr::reset().

不过,我想知道是否以及如何boost::move()帮助我解决这个问题。

0 投票
0 回答
253 浏览

c++ - unique_ptr 实现的 C++03 转发解决方法

我正在开发一个 C++03 项目,该项目需要借助 Boost.Move/其他 Boost 库来转发语义。该项目的目标是以向前兼容的方式向 C++11 提供 unique_ptr。

C++11 标准需要以下构造函数:

它从 u 传输存储的数据(基本上是 ptr_val = u.release()),并将:

  • 如果 E 是引用类型,将 u 的删除器复制到 *this。
  • 否则,将 u 的删除器移动到 *this。

在测试中,这段代码似乎“工作”:

是否有任何重要的角落案例不包括在内?

0 投票
1 回答
761 浏览

c++ - 将 C++11 std::thread 移植到 boost::thread 编译问题

我正在尝试使用 boost::thread 将 C++11 std::thread 代码移植到 VC9 (VS 2008)。下面的“等效”C++11 代码在 msvc12 上编译得很好:

我想使用 msvc9 编译器和 Boost 1.55 将代码移植到 C++03。如何解决以下编译错误:

编译错误是:

0 投票
1 回答
362 浏览

c++ - 在 gcc 4.6.4 中编译 boost::move

我有一个使用 Boost::move 移动锁的函数 -

我可以使用带有 -std=c++11 或 -std=c++0x 标志的 gcc 4.7.3 来编译此代码。但是,对于 gcc 4.6.4,即使使用 -std=c++0x 标志,此代码也会失败。有想法该怎么解决这个吗?

用于使用 C++11 功能的 CMAKE 标志:

用于使用 C++0x 功能的 CMAKE 标志:

我在 gcc 4.6.4 中遇到的错误:

错误:'iter != ((indexing::skarf::SkarfDatabase*)this)->indexing::skarf::SkarfDatabase::bucketFinderIndex.indexing:: 中的 'operator!=' 不匹配skarf::BucketIDT::Finder

::end with P = features::Descriptor, T = long unsigned int’ /home/rahulg/ripe/src/index/skarf/SkarfDatabase.hpp:141:40:注意:候选人是:/usr/local/include /boost/smart_ptr/shared_array.hpp:264:31: 注意:模板 bool boost::operator!=(boost::detail::sp_nullptr_t, const boost::shared_array&) /usr/local/include/boost/smart_ptr/shared_array .hpp:259:31: 注意:模板 bool boost::operator!=(const boost::shared_array&, boost::detail::sp_nullptr_t) /usr/local/include/boost/smart_ptr/shared_array.hpp:242:31 : 注意: 模板 bool boost::operator!=(const boost::shared_array&, const boost::shared_array&) /usr/local/include/boost/ptr_container/detail/void_ptr_iterator.hpp:185:21: 注意: 模板 bool boost ::operator!=(const boost::void_ptr_iterator&, const boost::void_ptr_iterator&) /usr/local/include/boost/blank.hpp:73:13: 注意:bool boost::operator!=(const boost::blank&, const boost::blank&) /usr/local/include/boost/blank.hpp:73:13: 注意:没有已知的参数 1 的转换来自“索引” ::skarf::BucketIDT::iterator 到“const boost::blank” /usr/local/include/boost/range/sub_range.hpp:161:17:注意:模板 bool boost::operator! =(const boost::sub_range&, const boost::sub_range&) /usr/local/include/boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=(const boost::function10&, const boost::function10&) /usr/local/include/boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=(const boost::function9&, const boost::function9&) /usr /local/include/boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=(const boost::function8&, const boost::function8&) /usr/local/include/boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=(const boost::function7&, const boost::function7&) /usr/local/include /boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=(const boost::function6&, const boost::function6&) /usr/local/include/boost/function/function_template.hpp :1031:8: 注意: 模板 void boost::operator!=(const boost::function5&, const boost::function5&) /usr/local/include/boost/function/function_template.hpp:1031:8: 注意: 模板void boost::operator!=(const boost::function4&, const boost::function4&) /usr/local/include/boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=( const boost::function3&, const boost::function3&) /usr/local/include/boost/function/function_template.hpp:1031:8: 注意:模板 void boost::operator!=(const boost::function2&, const boost::function2&) /usr/local/include/ boost/function/function_template.hpp:1031:8: 注意: 模板 void boost::operator!=(const boost::function0&, const boost::function0&) /usr/local/include/boost/function/function_template.hpp: 1031:8: 注意: 模板 void boost::operator!=(const boost::function1&, const boost::function1&) /usr/local/include/boost/function/function_base.hpp:872:3: 注意: 模板类型名boost::enable_if_c::value>::value, bool>::type boost::operator!=(boost::reference_wrapper, const boost::function_base&) /usr/local/include/boost/function/function_base.hpp: 863:3:注意:模板类型名 boost::enable_if_c::value>::value, bool>::type boost::operator!=(const boost::function_base&, boost::reference_wrapper) /usr/local/include/boost/function/function_base.hpp:835:3: 注意:模板类型名 boost::enable_if_c::value>::value, bool> ::type boost::operator!=(Functor, const boost::function_base&) /usr/local/include/boost/function/function_base.hpp:826:3: 注意:模板类型名 boost::enable_if_c::value>: :value, bool>::type boost::operator!=(const boost::function_base&, Functor) /usr/local/include/boost/function/function_base.hpp:764:13: 注意:bool boost::operator! =(boost::detail::function::useless_clear_type*, const boost::function_base&)function_base&) /usr/local/include/boost/function/function_base.hpp:826:3: 注意:模板类型名 boost::enable_if_c::value>::value, bool>::type boost::operator!=(const boost::function_base&, Functor) /usr/local/include/boost/function/function_base.hpp:764:13: 注意: bool boost::operator!=(boost::detail::function::useless_clear_type*, const boost ::function_base&)function_base&) /usr/local/include/boost/function/function_base.hpp:826:3: 注意:模板类型名 boost::enable_if_c::value>::value, bool>::type boost::operator!=(const boost::function_base&, Functor) /usr/local/include/boost/function/function_base.hpp:764:13: 注意: bool boost::operator!=(boost::detail::function::useless_clear_type*, const boost ::function_base&)

0 投票
1 回答
1261 浏览

c++ - 为什么将 boost::move() 的返回值分配给非常量引用在 C++0x 模式下失败但在 C++03 模式下有效

以下是可用于重现该问题的源代码:

在 GCC 中使用 C++03 模式时,代码编译没有任何问题。另一方面,使用 C++0x 模式会产生以下错误:

这种失败的原因是什么?有没有办法在 C++11 模式下解决它?

上面提供的代码是我在通用代码中使用的简化。因此需要这样的组合(boost::move+ non-const ref 作为返回值类型)。

我使用 gcc v4.6.3、Boost 1.54.0 和 Ubuntu 12.04 (i386)。

更新 1:我使测试用例更加真实。目标是拥有一个通用函数,它在锁下调用函子并通过 boost::move() 返回函子返回的值,因为返回值的类型可能是可移动但不可复制的(在某些情况下是正确的)我对 ) 的调用call_under_lock()

clang 3.5.1~(exp)更新 2:事实证明,在 C++11 模式下使用时可以观察到类似的问题:

更新 3:这个话题也在 boost-users 邮件列表 [1] 上讨论过。

[1] http://boost.2283326.n4.nabble.com/move-differences-between-results-in-C-03-and-C-11-modes-td4659264.html

0 投票
1 回答
410 浏览

c++11 - 使用带有移动语义的 C++11 - 没有标准库(和 Boost.smart_ptr)

我正在从事嵌入式项目,使用Zephyr RTOS和 ARM 嵌入式微控制器,如 STM32 Nucleo 系列(Cortex M4/0)。

最近,由于最近版本的 Zephyr 对 C++ 的支持显着改进,我正在考虑从 C 开发转向现代 C++。

默认情况下,Zephyr 包含 C 标准库,但不包含 Cpp 的 STD。

Zephyr 实际上添加了对 Cpp 标准库的可选支持,这似乎工作得很好。然而,它需要大量的闪存使用(我的微控制器闪存中接近 65%,没有我的代码!)。

因此,我正在尝试寻找在没有实际标准库(包括内存/实用程序头文件)的情况下使用现代 C++ 的替代方法。

我考虑过将Boost.smart_ptr用于智能指针,但我想知道在我的情况下我应该如何使用移动语义 (std::move)。

我应该使用Boost.Move模块吗?即使我使用的是 C++1x 而不是 C++0x?

我应该自己使用从 T& 到 T&& 的直接转换来实现移动语义吗?