草庐IT

c++ - 当 vector 增长时如何强制执行 move 语义?

我有一个std::vector某个类A的对象。该类是非平凡的,并且定义了复制构造函数和move构造函数。std::vectormyvec;如果我用A对象填充vector(使用例如myvec.push_back(a)),则使用复制构造函数A(constA&)实例化vector中元素的新拷贝。我能否以某种方式强制使用A类的move构造函数来代替? 最佳答案 您需要使用noexcept通知C++(特别是std::vector)您的move构造函数和析构函数不会抛出。然后当vector增长时会调用move构造函数。这是如何声明和实现受std

c++ - initializer_list和move语义

我可以将元素移出std::initializer_list吗?#include#includetemplatevoidfoo(std::initializer_listlist){for(autoit=list.begin();it!=list.end();++it){bar(std::move(*it));//kosher?}}由于std::intializer_list需要特别注意编译器,并且不像C++标准库的普通容器那样具有值语义,因此我宁愿安全而不愿后悔。 最佳答案 不,这不会按预期工作;您仍然会得到副本。我对此感到非常惊讶

c++ - 什么时候应该在函数返回值上使用 std::move?

这个问题在这里已经有了答案:c++11Returnvalueoptimizationormove?[duplicate](4个回答)关闭5年前。在这种情况下structFoo{};Foomeh(){returnstd::move(Foo());}我很确定move是不必要的,因为新创建的Foo将是一个xvalue。但是在这种情况下呢?structFoo{};Foomeh(){Foofoo;//dosomething,butknowingthatfoocansafelybedisposedof//butdoesthecompilernecessarilyknowit?//wemayhave

c++ - Clang 和 GCC vs MSVC 和 ICC : Is a static_assert in the copy/move constructor required to work, 如果复制/移动省略也可以应用?

我的模板结构的移动构造函数中有一个static_assert。编译器是否需要考虑这个static_assert,即使复制省略是可能的?这是精简的场景:#includetemplatestructX{X(X&&){static_assert(std::is_same::value,"IntentionalFailure");}};autoimpl()->X;autotest()->decltype(impl()){returnimpl();}intmain(){test();}GCC和Clang同意评估static_assert并且编译失败。另一方面,MSCV和ICC可以很好地编译代码。

c++ - Clang 和 GCC vs MSVC 和 ICC : Is a static_assert in the copy/move constructor required to work, 如果复制/移动省略也可以应用?

我的模板结构的移动构造函数中有一个static_assert。编译器是否需要考虑这个static_assert,即使复制省略是可能的?这是精简的场景:#includetemplatestructX{X(X&&){static_assert(std::is_same::value,"IntentionalFailure");}};autoimpl()->X;autotest()->decltype(impl()){returnimpl();}intmain(){test();}GCC和Clang同意评估static_assert并且编译失败。另一方面,MSCV和ICC可以很好地编译代码。

PHP - 将文件 move 到服务器上的不同文件夹中

我需要允许我网站上的用户在上传图片后从服务器上删除他们不再需要的图片。我以前在PHP中使用unlink函数,但后来被告知这可能非常危险并且是一个安全问题。(下面是之前的代码:)if(unlink($path.'image1.jpg')){//deleted}相反,我现在只想将文件move到不同的文件夹中。这必须能够在他们首次上传文件后很长时间才能完成,以便他们在任何时候登录到他们的帐户。如果我有存储用户图像的主文件夹:user/然后在其中一个名为del的文件夹中放置不需要的图像:user/del/是否有将文件move到不同文件夹的命令?所以说:user/image1.jpgmove到/

c++ - 为什么包含 std::stringstream 的类的 move 语义会导致编译器错误?

如何使这个简单的类可move?我认为是正确的只会产生一堵错误的墙......#include#include#includeclassmessage{public:message()=default;//Moveconstructormessage(message&&other):stream_(std::move(other.stream_))//Nope{}//Moveassignmentmessage&operator=(message&&other){if(this!=&other){stream_=std::move(other.stream_);//Nope#2}retur

c++ - 为什么包含 std::stringstream 的类的 move 语义会导致编译器错误?

如何使这个简单的类可move?我认为是正确的只会产生一堵错误的墙......#include#include#includeclassmessage{public:message()=default;//Moveconstructormessage(message&&other):stream_(std::move(other.stream_))//Nope{}//Moveassignmentmessage&operator=(message&&other){if(this!=&other){stream_=std::move(other.stream_);//Nope#2}retur

c++ - 何时在 C++11 中使类型不可 move ?

我很惊讶这没有出现在我的搜索结果中,考虑到C++11中move语义的有用性,我想有人会问过这个问题:我什么时候必须(或者对我来说是个好主意)在C++11中使一个类不可move?(原因其他不是与现有代码的兼容性问题。) 最佳答案 Herb的回答(在编辑之前)实际上给出了一个不应该可move的类型的好例子:std::mutex.操作系统的native互斥体类型(例如POSIX平台上的pthread_mutex_t)可能不是“位置不变的”,这意味着对象的地址是其值的一部分。例如,操作系统可能会保留一个指向所有已初始化互斥对象的指针列表。如

c++ - 为什么我要 std::move 一个 std::shared_ptr?

我一直在看Clangsourcecode我发现了这个片段:voidCompilerInstance::setInvocation(std::shared_ptrValue){Invocation=std::move(Value);}我为什么要std::move一个std::shared_ptr?转让共享资源的所有权有什么意义吗?我为什么不这样做呢?voidCompilerInstance::setInvocation(std::shared_ptrValue){Invocation=Value;} 最佳答案 我认为其他答案没有足够强