rvalue-reference相关内容
我有一个类被查询一个内部状态对象: class State {...} //有复制和移动构造函数 class Processor { private: std :: unique_ptr州; public: void process(...) { State newState; ... //创建这个新状态 state.reset(new Sta
..
有传递构造函数参数的首选做法吗?特别是如果这些构造函数参数用于初始化成员变量。 一个简化的例子。 class Example { public: 示例(/ * type-1 * / str,/ * type-2 * / v): m_str str), m_v(v) {} / *其他方法* / private: std :: string m_str ;
..
我不知道(只是出于好奇)为什么操作符重载是不允许在C ++的指针。我的意思是这样的: Vector2d * operator +(Vector2d * a,Vector2d * b){return new Vector2d(ax + bx ,ay + by); } Vector2d * a = new Vector2d(1,1); Vector2d * b = new Vect
..
请考虑以下函数 accept ,它使用类型 T 的“通用引用”,并将其转发到 parse()函数对象,对于左值重载和右值重载: template void accept(T&& arg) { parse()(std :: forward(arg),0); //复制或移动,取决于arg的价值 } template
..
目前我正在玩的右值引用(C ++ 11,g ++与gnu ++ x0),我想在我的类中实现移动语义,因为它只是感觉“正确”。 我需要重载通常会接受const lvalue引用的每个函数,以从右值引用中获益吗? 让我们说这是我的示例类: class Person { public: Person() Person(std :: string& name); Perso
..
我读过几篇关于&&我只是好奇如果有: void fnc_1(int& p) { // ... } void fnc(int& r) { fnc_1(r); //我想/一世?调用它像这样:fnc_1(std :: forward(r)) } fnc_1(r)只要传递'r'就足够了? r 是一个左值,就像任何其他变量,不管类型。是的,没错,
..
C ++标准定义了以下函数已删除: template void cref(const T&&)= delete; 这是为了帮助确保函数不被滥用,不允许它们绑定到临时值 const&&& 绑定到所有rvalues prval
..
如果我从 b 中移动构建 a ,仍然需要 破坏 b ,或者我可以不这样做吗? 可选 模板。摘录: 〜可选() { if(初始化) { reinterpret_cast(data) - >〜T(); } } 可选(可选& o):已初始化(o.initialized) { if(已初始化) { new(data)T(std :: move(*
..
我有一个回调实现使用右值引用存储参数,它与gcc工作正常,但无法在VS 2010中编译一些代码。简短版本: #include class B { public: virtual void execute()= 0; }; template
..
我有一个回调实现使用右值引用存储参数,它与gcc工作正常,但无法在VS 2010中编译一些代码。简短版本: #include class B { public: virtual void execute()= 0; }; template
..
例如,虽然rvalue “Hello,World”的类型为 const char * 。 const char * 本身并不意味着指针不能被改变。不能更改 char * const 指针,但这不是编译器构造的。 意味着对于包含 const char * 的任何容器,数据应该通过除C ++的移动语义之外的其他方法复制?有没有办法只是移动编译器构造的字符串并保留所有其他字符串单独? 例如,
..
template void f(U&& v) { std :: cout(int(1)); } }
..
// code from https://skillsmatter.com/skillscasts/2188-move-semanticsperfect-forwarding-and-rvalue-references class Widget { public: Widget(Widget&& rhs) :pds(rhs.pds)//获取源值 { rhs.pds = nullptr
..
我用GCC,Clang,ICC和VS测试了以下代码: void f(){} void g(void(&&)()){} int main(){ g(f); } 正如我们所看到的, g 采用右值引用,但 f 是一个左值,一般来说,右值引用不能绑定到左值。这正是ICC抱怨的 : 错误:右值引用不能绑定到左值 VS也会发生错误,但是出
..
以下代码在gcc 4.8.1上没有问题地编译: #include
..
在本文中 http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1877 : T& T :: operator =(T const& x)// x是对源的引用 { T tmp(x); // copy construction of tmp does the hard wo
..
我想知道是否有任何方法来区分函数调用(使用数组作为参数)如下代码所示: #include #include template void foo_array(const char(& data)[Size]) { std :: cout
..
为什么我不能从对中返回unique_ptr? #include #include using namespace std; unique_ptr get_value(){ pair,int> p(unique_ptr(new i
..
更新问题为什么这两个右值引用示例具有不同的行为?: 源代码: int a = 0; auto&&& b = a ++; ++ a; cout
..
我想实现一个函数填充一个向量,然后返回一个右值引用。我累了像: std :: vector &&& fill_list(){ std :: vector res; ... do something to fill res ... return res; } int main(int argc,char ** argv){ std :: vect
..