草庐IT

move-constructor

全部标签

c++ - 使用 g++ 编译 C++ 时出现 'hides constructor for' 警告是什么意思?

使用以下代码:#includestructmy_struct{inta;intb;my_struct();};my_struct::my_struct(void){printf("constructor\n");}voidmy_struct(void){printf("standardfunction\n");}intmain(intargc,char*argv[]){structmy_structs;s.a=1;s.b=2;printf("%d-%d\n",s.a,s.b);return0;}我在使用g++-Wshadowmain.cpp编译时收到警告:main.cpp:15:20:

c++ - 使用 C++0x : call to deleted constructor of 时的 clang++ 错误消息

您好,我已经将我的Xcode升级到4.2版,并将clang++升级到以下版本:Appleclangversion3.0(tags/Apple/clang-211.10.1)(basedonLLVM3.0svn)Target:x86_64-apple-darwin11.2.0Threadmodel:posix当尝试使用clang-std=c++0x编译以下代码时#include#include#includeclassilpConstraintImpl{public:virtual~ilpConstraintImpl(){}};classilpConstraint{public:ilpC

c++ - 为什么我可以 std::move 流右值引用到左值引用?

据我了解C++11引用,我不应该能够将右值引用绑定(bind)到(非常量)左值引用,因为前者可能绑定(bind)到临时对象,而后者绝不能绑定(bind)到一个临时的。但是我发现这种奇怪的行为与临时流对象有关(我尽可能地减少了)structDummy{};templateStream&operatorvoidpass(Stream&&s){std::move(s)lvalueconversion?}#includeintmain(){pass(std::fstream("test",std::ios::out));}如果我写s在线(X),C++在(A)行提示,说error:invalid

c++ - 使类不可 move 的用例(如果有)是什么?

考虑一种使类不可复制的经典方法://similartoboost::noncopyableclassnoncopyable{protected:constexprnoncopyable()=default;noncopyable(constnoncopyable&)=delete;noncopyable&operator=(constnoncopyable&)=delete;};classc:privatenoncopyable{/*...*/};由于声明任何复制操作会阻止自动生成move操作,这会自动使所有派生类(默认情况下)也不可move(因此noncopyable的全名将是non

c++ - 在 C++11 中,虚函数能否通过 move 语义有效地返回一个大值?

通常,这会被优化为不涉及复制大值(因为std::vector启用了move语义):std::vectormakeABigThing(){std::vectorlarge_thing(1000,0);returnlarge_thing;}如果函数是虚方法,是否也可以用同样的方式优化:structFoo{virtualstd::vectormakeABigThing(){std::vectorlarge_thing(1000,0);returnlarge_thing;}};即,即使在运行时选择了被调用的函数,move语义是否也适用? 最佳答案

c++ - 错误 : definition of implicitly declared copy constructor

我目前正在处理的QtC++项目有问题。这是我要介绍的一个新部分,但我发现它有点令人困惑。我创建了一些由股票、债券和储蓄类继承的Assets类。这一切都很好。然后我创建了一个名为AssetList的类,它派生了QList,这个类是我发现问题的地方。这是我目前的代码。资源列表.h#ifndefASSET_LIST_H#defineASSET_LIST_H#include"Asset.h"#includeclassAssetList:publicQList{public:AssetList(){}~AssetList();booladdAsset(Asset*);Asset*findAsse

c++ - MI 和隐式复制构造函数错误(原为 : Under what conditions can a template be the copy constructor?)

我很确定这个问题的答案是,“模板永远不可能成为复制构造函数。”不幸的是,我只花了3个小时弄清楚为什么我会收到有关递归的警告,跟踪它到复制构造函数,看着调试器发疯,不让我看递归代码,最后跟踪到一个基础构造函数中缺少“&”。你看,我有一个复杂的基于策略的设计主机,它已经运行了一段时间了。我着手将两个策略合二为一并遇到了一个递归复制构造函数。将其缩小为一个策略,该策略需要提供一个构造函数,该构造函数可以采用一种XXX概念作为其参数,但在这种情况下,我只是放弃它。所以我写了structmy_policy{templatemy_polity(Tconst){}//missing'&'...oop

C++ move 语义 : why copy assignment operator=(&) is called instead of move assignment operator=(&&)?

我有以下代码:#include#includeusingstd::cout;structSomeType{SomeType(){}SomeType(constSomeType&&other){cout我希望move构造函数调用move赋值运算符。下面是这个程序的输出:SomeType(SomeType&&)operator=(constSomeType&)operator=(SomeType&&)如您所见,move赋值运算符已成功调用,但在move构造函数内分配给*this时未成功调用。为什么会发生这种情况,我能以某种方式解决它吗? 最佳答案

c++ - GCC 4.9 的 unordered_set 和 std::move

当在GCC4.9上移出一个unordered_set,然后重新使用移出的对象时,我在添加到它时得到除以零。我的理解(来自http://en.cppreference.com/w/cpp/utility/move)是可以使用移出的对象,前提是不违反其先决条件。在移出的集合上调用clear()很好(这在前提条件的上下文中是有意义的),但我不清楚我添加新元素是否违反了任何前提条件。示例代码:#includeusingnamespacestd;voidfoo(unordered_set&&a){unordered_setcopy=std::move(a);}voidtest(){unorder

c++ - 为共享指针 move 构造函数和 = 运算符

说,我有一个类:classGameObject///headerfile{....std::shared_ptrtransform;}///cppfile//CopyCtorGameObject::GameObject(constGameObject&rhs):transform(rhs.transform){}//MoveCTorGameObject::GameObject(GameObject&&rhs):transform(std::move(rhs.transform)){}为具有shared_ptr成员变量的类创建move构造函数是否正确?或者我是否需要在move后调用rhs