草庐IT

c++ - Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to ".cpp"?

只要我不将构造函数(B)的定义移动到标题B.h中,代码就可以工作。B.hclassImp;//imp;B();//B.cpp#include"B.h"#include"Imp.h"B::B(){}~B::B(){}Imp.hclassImp{};Main.cpp(编译我)#include"B.h"Error:deletionofpointertoincompletetypeError:useofundefinedtype'Imp'C2027我能以某种方式理解必须将析构函数移动到.cpp,因为可能会调用Imp的解构:-deletepointer-of-Imp;//somethinglik

c++ - 编译器什么时候不能使用 RVO 或 NRVO?

当编译器不能使用RVO时,move语义很有用。和NRVO.但是在什么情况下编译器不能使用这些特性呢? 最佳答案 答案是它取决于编译器和情况。例如。控制流分支可能会混淆优化器。Wikipedia举个例子:#includestd::stringf(boolcond=false){std::stringfirst("first");std::stringsecond("second");//thefunctionmayreturnoneoftwonamedobjects//dependingonitsargument.RVOmightno

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++ - 通过 `&&` 传递的参数对非构造函数有用吗?

一个人可能有一个函数voidsetData(std::stringarg);并通过setData(std::move(data));调用它,从而调用move构造函数,他会为voidsetData(std::string&&arg);做同样的事情(除了他会被迫将数据移入其中)。编译器不能决定是否对简单情况执行move,是否已经这样做了?所以我的问题是:不仅要为编译器而且要为通用代码(例如为其他开发人员创建的API成员)使用&&? 最佳答案 优化r值比较voidsetData(std::stringarg)和voidsetData(st

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()之外