草庐IT

nothrow_move_constructible

全部标签

c++ - 使用哪个 : move assignment operator vs copy assignment operator

我似乎不明白为什么要使用移动赋值运算符:CLASSA&operator=(CLASSA&&other);//moveassignmentoperator结束了,复制赋值运算符:CLASSA&operator=(CLASSAother);//copyassignmentoperator移动赋值运算符仅采用r值引用,例如CLASSAa1,a2,a3;a1=a2+a3;在复制赋值运算符中,other可以是使用复制构造函数或移动构造函数的构造函数(如果other是用右值初始化的,它可以是移动构造的——如果move-constructor定义了——)。如果它是copy-constructed,我

c++ - std::transform 和 move 语义

我正在使用Boost.Filesystem在目录中创建文件列表。我使用boost::filesystem::recursive_directory_iterator和std::copy将每个路径放入std::vector作为boost::filesystem::directory_entry对象。不过,我希望将文件作为std::strings输出,所以我执行了以下操作(\n以避免使用std::vectorbuffer;//filledwithpaths...std::vectorbuffer_native(buffer.size());//transformdirectory_entr

c++ - ‘operator=’ 的模糊重载与 c++11 std::move and copy and swap idiom

我收到以下错误:[matt~]g++-std=c++11main.cpp-DCOPY_AND_SWAP&&./a.outmain.cpp:Infunction‘intmain(int,constchar*const*)’:main.cpp:101:24:error:ambiguousoverloadfor‘operator=’in‘move=std::move((*©))’main.cpp:101:24:note:candidatesare:main.cpp:39:7:note:Test&Test::operator=(Test)main.cpp:52:7:note:Test&

c++ - 将代码从 C++03 迁移到 C++11 : should I be cautious about the implicit default move constructor?

我有一个代码库,我想从C++03切换到C++11。据我所知,某些类将通过具有隐式默认移动构造函数(以及随之而来的移动赋值运算符)而从更改中受益。虽然我完全同意(我什至认为这是一件好事),但我有点担心这种隐式构造函数可能对我拥有的某些不可复制类产生的影响。我举的一个例子是一个类,它包装了libiconv的iconv_t句柄以利用RAII。更明确地说,类如下:classiconv_wrapper{public:iconv_wrapper():m_iconv(iconv_open()){}~iconv_wrapper(){iconv_close(m_iconv);}private://Not

c++ - 在 C++11 中从 std::deque move 一个元素

我们知道std::deque::front()返回对双端队列第一个元素的引用。我想知道这段代码是否总是安全的://dequeoflambdasdeque>funs;//thenissomeotherplace://takealockm.lock();autof=move(funs.front());//movethefirstlambdainffuns.pop_front();//removetheelementfromdeque//nowthevalueisholdbyfm_.unlock();//unlocktheresorcef();//executef我已经使用gcc-4.9尝

c++ - 为什么 `T(const T&&)` 被称为 move 构造函数?

[C++11:12.8/3]:Anon-templateconstructorforclassXisamoveconstructorifitsfirstparameterisoftypeX&&,constX&&,volatileX&&,orconstvolatileX&&,andeithertherearenootherparametersorelseallotherparametershavedefaultarguments(8.3.6).[..]为什么采用const右值引用的构造函数被标准称为“move构造函数”?一定it'sself-evident那thisprohibitsmea

c++ - std::unique_ptr::release() 与 std::move()

我有一个表示运行时上下文并构建树的类,树根保存在unique_ptr中。构建树完成后,我想提取树。这是它的样子(不可运行,这不是调试问题):classContext{private:std::unique_ptrroot{newNode{}};public://imagineaconstructor,attributesandmethodstobuildatreestd::unique_ptrextractTree(){returnstd::move(this->root);}};所以我使用std::move()从Context实例中提取根节点。但是,除了使用std::move()之外

c++ - std::move 是否与左值引用一起使用? std::move 如何在标准容器上工作?

#includestructA{inta[100];};voidfoo(constA&a){std::vectorvA;vA.push_back(std::move(a));//howdoesmovereallyhappen?}intmain(){Aa;foo(a);}上面的代码编译正常。现在到处都写着move避免复制。以下是我的查询:move在处理左值时真的有效吗[非]-const引用?即使有“右值引用”,对象复制时如何避免复制像上面一样插入标准容器中?例如voidfoo(A&&a){//supposeweinvokethisversionstd::vectorvA;vA.push_

c++ - "Expected ' (' for function-style cast or type construction"错误是什么意思?

我收到错误“Expected'('forfunction-stylecastortypeconstruction”,我已尽力在线研究此错误的含义,但无法找到导致此错误的任何文档错误。我在StackOverflow上发现的所有相关问题都修复了特定的代码片段,并且没有更笼统地解释导致错误的原因。这些包括Expected'('forfunction-stylecastortypeconstruction答案突出了代码的几个问题。究竟是哪个问题导致了错误尚不清楚。c++Xcodeexpected'('forfunction-stylecastortypeconstruction在主函数中定义函

C++ 右值引用和 move 语义

所以我昨天在youtube上观看了c++视频,发现了一个关于C++-11右值引用和move语义的视频。我想我从广义上理解了这个概念,但是今天当我和助教一起检查我的代码时,他问我为什么没有在下面的代码中给我们一个引用(比如std::pair&p)。在这种情况下,我根本没有考虑过,但当他问起时,我想起了视频中所说的“在C++-11中,你通常应该使用按值传递。”因此我的问题是:在下面的代码中,std::pairp像std::pair&p一样过得更好或不?是否会使用move语义,它会有所作为吗?IPAddressNameServer::lookup(constHostName&host)con