让我说我有这个异常类:
struct MyException : public std::exception
{
MyException(const std::exception &exc) : std::exception(exc)
{
cout << "lval\n";
}
MyException(std::exception &&exc) : std::exception(std::forward<std::exception>(exc))
{
cout << "rval\n";
}
};
...
...
try
{
throw std::exception("Oh no!");
// above is rvalue since it's got no name, what if the throw is made as
// std::exception lvalExc("Oh wierd!");
// throw lvalExc;
// if the throw is made thus, how can it be caught by catch(std::exception &&exc)?
}
catch(std::exception &&rValRef)
{
cout << "rValRef!\n";
throw …Run Code Online (Sandbox Code Playgroud) 我想实现一个填充向量然后返回右值引用的函数.我厌倦了:
std::vector<int> &&fill_list() {
std::vector<int> res;
... do something to fill res ...
return res;
}
int main(int argc, char **argv) {
std::vector<int> myvec = fill_list();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我收到以下错误:
error: invalid initialization of reference of type 'std::vector<int>&&' from expression of type 'std::vector<int>'
Run Code Online (Sandbox Code Playgroud)
总而言之,正确的做法是怎样的?我认为我还没有获得右值参考.
以下程序不构建VS11 beta,gcc 4.5或clang 3.1
#include <thread>
#include <memory>
int main() {
std::unique_ptr<int> p;
std::thread th([](std::unique_ptr<int>) {
},std::move(p));
th.join();
}
Run Code Online (Sandbox Code Playgroud)
这是因为参数类型不可复制,但实现尝试复制它.
据我所知,这个程序很好,应该可行.对std :: thread的要求似乎意味着可移动的,不可复制的参数应该在这里工作.具体来说,它表示可调用对象和每个参数应满足MoveConstructible要求,并且INVOKE(DECAY_COPY(std::forward<F>(f)),DECAY_COPY(std::forward<Args>(args))...)应该是一个有效的表达式.
在这种情况下,我认为表达式可以解决这样的问题:
template <class T> typename std::decay<T>::type decay_copy(T&& v)
{ return std::forward<T>(v); }
std::unique_ptr<int> p;
auto f = [](std::unique_ptr<int>) {};
decay_copy(f)(decay_copy(std::move(p)));
Run Code Online (Sandbox Code Playgroud)
我不认为这应该涉及到副本p.gcc至少可以编译这个表达式,虽然VS11没有.
我有一个类'记住'对某个对象的引用(例如整数变量).我不能让它引用一个立即被破坏的值,我正在寻找一种方法来保护我班级的用户不会这样做.
rvalue-reference重载是防止临时传入的好方法吗?
struct HasRef {
int& a;
HasRef(int& a):a(a){}
void foo(){ a=1; }
};
int main(){
int x=5;
HasRef r1(x);
r1.foo(); // works like intended.
HasRef r2(x+4);
r2.foo(); // dereferences the temporary created by x+4
}
Run Code Online (Sandbox Code Playgroud)
私有rvalue超载会吗?
struct HasRef {
int& a;
HasRef( int& a ):a(a){}
void foo(){ a=1; }
private:
HasRef( int&& a );
};
... HasRef r2(x+1); // doesn't compile => problem solved?
Run Code Online (Sandbox Code Playgroud)
我有没有看到任何陷阱?
c++ pass-by-reference rvalue-reference pass-by-rvalue-reference
我有2个重载函数 - 一个采用L值,另一个采用R值.目的是使函数可以像:
Obj obj;
foo(obj);
Run Code Online (Sandbox Code Playgroud)
要么:
foo(Obj());
Run Code Online (Sandbox Code Playgroud)
所以,我写了2个重载函数:
template <class T>
void foo(T& v)
{
/* ... function body code goes here ... */
}
template <class T>
void foo(T&& v)
{
foo(v);
}
int main()
{
foo(int(5));
}
Run Code Online (Sandbox Code Playgroud)
R值过载仅需要委托给L值过载.我理解它的方式,一旦我在函数体内,任何使用v都会给我一个L值参考,除非我特意使用std::move或std::forward.因此foo(v),在R值重载内调用应该自动调用L值版本(而不是递归).
但是,编译器抱怨模棱两可:
test.cpp: In function ‘void foo(T&&) [with T = int]’:
test.cpp:305:12: instantiated from here
test.cpp:299:2: error: call of overloaded ‘foo(int&)’ is ambiguous
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这是模棱两可的.到呼叫foo()R值过载内应明确调用L值的版本.那为什么不编译呢?
我有下面的数字向量模板类(用于数值计算的向量).我正在努力编写D=A+B+C所有变量都是Vector对象的地方.A,B并且C不应该修改.我的想法是使用,Vector operator+(Vector&& B)以便在(希望)Vector返回Rvalue之后B+C,所有后续添加都存储在该对象中,即为所有后续添加窃取Rvalue的存储.这是为了消除新对象的创建和所需的存储.
我的问题是,我可以从调用的每个函数的输出语句中看到,Vector operator+(Vector&& B)从未调用过.我不明白为什么,因为,如果我有一个重载虚拟函数foo(Vector& B)与foo(Vector&& B)和尝试foo(A+B+C),那么第二个功能是完全相同称为我所希望的.
很抱歉这个冗长的问题,但这是我在这里的第一个问题,我想尽量保持清醒.
关于我明显做错了什么或为什么我不应该尝试这个的任何建议,将不胜感激.
template <typename T>
class Vector
{
int n;
T* v;
Vector();
~Vector();
Vector(const Vector& B);
Vector(Vector&& B);
inline Vector operator+(const Vector& B) const;
inline Vector operator+(Vector&& B) const;
};
template <typename T>
Vector<T>::Vector(const Vector<T>& B)
{
...
}
template <typename T>
Vector<T>::Vector(Vector<T>&& B)
{
...
} …Run Code Online (Sandbox Code Playgroud) 我理解rvalue引用是如何工作的,但我不确定它们如何与STL中的迭代器一起工作.这是我想要的东西:
void insertList(std::list<int>& L, std::list<int>&& R, std::list<int>::iterator insertPoint)
{
L.insert(insertPoint, R.begin(), R.end()); // want to use move semantics
}
Run Code Online (Sandbox Code Playgroud)
现在我知道std :: list有一个splice方法.但我想知道这是否可行.它也可以用于deque吗?
这是一些示例代码:
#include <iostream>
class Foo
{
public:
explicit Foo(int x) : data(x) {};
Foo& operator++()
{
data += 1;
return *this;
}
void *get_addr()
{
return (void*)this;
}
friend Foo operator + (const Foo& lhs, const Foo& rhs);
friend std::ostream& operator << (std::ostream& os, const Foo& f);
private:
int data;
};
std::ostream& operator << (std::ostream& os, const Foo& f)
{
return (os << f.data);
}
Foo operator + (const Foo& lhs, const Foo& rhs)
{
return Foo(lhs.data + rhs.data);
} …Run Code Online (Sandbox Code Playgroud) 如果我写下面的代码:
#include <iostream>
using namespace std;
int main()
{
cout << &(int &&)123 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后g++抱怨:
foo.cc: In function ‘int main()’:
foo.cc:7:20: error: taking address of xvalue (rvalue reference)
Run Code Online (Sandbox Code Playgroud)
好的,多亏了什么是右值,左值,x值,glvalues和prvalues?我得到一个xvalue意味着它即将"过期",这是有道理的.但是现在如果我这样做:
#include <iostream>
using namespace std;
int main()
{
const int &x = (int &&)123;
cout << &x << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这"工作"很好,将打印一个地址.所以,我有几个问题:
通常有一种方法可以知道右值参考的生命周期吗?
我有下课
class widget {
// The methods only print their name, i.e. c'tor, destructor etc.
public:
widget();
widget(const widget&);
widget(widget&&);
~widget();
auto operator=(const widget&) -> widget&;
auto operator=(widget&&) -> widget&;
};
Run Code Online (Sandbox Code Playgroud)
我在下面的代码中使用它
#include "widget.h"
auto main() -> int {
widget c(std::move(widget()));
c = std::move(widget());
return 0;
};
Run Code Online (Sandbox Code Playgroud)
由此产生的行为对我来说是可以理解的.在第一次调用中构造一个小部件,然后调用移动构造函数并在临时小部件上调用析构函数.
第二个调用也是这样,期望调用移动赋值运算符而不是移动构造函数.离开main方法,调用析构函数c.
现在有趣的是:
#include "widget.h"
auto main() -> int {
widget c((widget()));
c = widget();
return 0;
};
Run Code Online (Sandbox Code Playgroud)
如果我省略了调用std::move,第一个案例就会停止工作并导致只有一个构造函数调用.而第二个案件仍然像以前一样工作.
我在这里错过了什么?为什么这两个函数调用对待它们的参数有所不同?我在gcc和clang上尝试过这个.
c++ ×10
rvalue-reference ×10
c++11 ×8
xvalue ×2
exception ×1
forwarding ×1
g++ ×1
move ×1
numerical ×1
rvalue ×1