如果我a从中移动构造b,是否仍然需要破坏b,或者我可以在不这样做的情况下逃脱?
在实施optional<T>模板期间,这个问题在我脑海中浮现.摘抄:
~optional()
{
if (initialized)
{
reinterpret_cast<T*>(data)->~T();
}
}
optional(optional&& o) : initialized(o.initialized)
{
if (initialized)
{
new(data) T(std::move(*o)); // move from o.data
o.initialized = false; // o.data won't be destructed anymore!
}
}
Run Code Online (Sandbox Code Playgroud)
当然,我可以initialized用三值枚举替换bool ,区分初始化,非初始化和移动.我只是想知道这是否是绝对必要的.
在Visual Studio 2010中实现的rvalue引用与C++ 11中指定的有什么区别?在Visual Studio 2010中使用重量值引用时,如果使用符合C++ 11的编译器编译,可能会使源无效或工作方式不同,是否有任何特别的缺陷?
我有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) 根据另一个答案,如果引用它的表达式是xvalue表达式,则右值引用不会延长临时值的生命周期.因为std::move返回一个右值引用,所以调用它的表达式是一个xvalue,所以下面的结果是一个悬空引用:
int main()
{
std::string&& danger = std::move(get_string()); // dangling reference !
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没关系.在std::move没有意义在这里; 它已经是一个左值.
但是这里我正在画一个空白.将xvalue表达式作为参数传递,完全标准使用std::move和rvalue引用有何不同?
void foo(const std::string& val);
// More efficient foo for temporaries:
void foo(std::string&& val);
int main()
{
std::string s;
foo(std::move(s)); // Give up s for efficiency
return 0;
}
Run Code Online (Sandbox Code Playgroud)
rvalue引用参数是否有一个特殊规则,它将延长临时的生命周期,无论它是prvalue还是xvalue?或者std::move调用表达式只是一个xvalue,因为我们传递了一个已经是rvalue的东西?我不这么认为,因为它总是返回一个右值引用,这是一个xvalue.我在这里很困惑.我想我错过了一些愚蠢的话.
这是一些示例代码:
#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) 为什么在带有通用引用参数的构造函数的类中不会出现右值优化?
http://coliru.stacked-crooked.com/a/672f10c129fe29a0
#include <iostream>
template<class ...ArgsIn>
struct C {
template<class ...Args>
C(Args&& ... args) {std::cout << "Ctr\n";} // rvo occurs without &&
~C(){std::cout << "Dstr\n";}
};
template<class ...Args>
auto f(Args ... args) {
int i = 1;
return C<>(i, i, i);
}
int main() {
auto obj = f();
}
Run Code Online (Sandbox Code Playgroud)
输出:
Ctr
Ctr
Dstr
Ctr
Dstr
Dstr
Run Code Online (Sandbox Code Playgroud) 我最近正在学习新的C++ 11功能.但是,我并不完全了解rvalues的一件事.
考虑以下代码:
string getText ()
{
return "Fabricati diem";
}
string newText = getText();
Run Code Online (Sandbox Code Playgroud)
调用以getText()创建复制到newText变量的r值.但这个右值究竟存储在哪里?复制后会发生什么?
我想知道标准的哪些部分在以下代码段中指定:
#include <memory>
class A { };
class B : public A { };
int main()
{
std::unique_ptr<B> bptr = std::make_unique<B>(); // (a)
std::unique_ptr<A> aptr = std::move(bptr); // (b)
std::unique_ptr<A> &aptr_r = bptr; // (c)
std::unique_ptr<A> &&aptr_rr = std::move(bptr); // (d)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(d)编制而且(c)没有编制.请在答案中包含标准的相关部分或适当参考.仅供参考,Ubuntu clang版本3.6.2-1(标签/ RELEASE_362/final)(基于LLVM 3.6.2)给了我
error: non-const lvalue reference to type 'unique_ptr<A>' cannot
bind to a value of unrelated type 'unique_ptr<B>'
std::unique_ptr<A> &aptr_r = bptr;
^ ~~~~
Run Code Online (Sandbox Code Playgroud)
和gcc(Ubuntu 5.2.1-22ubuntu2)5.2.1 20151010给了我
error: invalid initialization …Run Code Online (Sandbox Code Playgroud) #include <utility>
template <typename Container>
decltype(auto) index(Container &&arr, int n) {
return std::forward<Container>(arr)[n];
}
Run Code Online (Sandbox Code Playgroud)
进行函数调用:
#include <vector>
index(std::vector {1, 2, 3, 4, 5}, 2) = 0;
Run Code Online (Sandbox Code Playgroud)
当函数调用完成时,对象std::vector {1, 2, 3, 4, 5}将被销毁,为释放的地址赋值会导致未定义的行为。但是上面的代码运行良好,valgrind 什么也没检测到。也许编译可以帮助我制作另一个不可见的变量,例如
auto &&invisible_value {index(std::vector {1, 2, 3, 4, 5}, 2)};
invisible_value = 9;
Run Code Online (Sandbox Code Playgroud)
如果我的猜测不正确,我想知道为什么为从函数返回的右值引用赋值是可行的,以及临时对象index(std::vector {1, 2, 3, 4, 5}, 2)何时会被销毁。
这个想法起源于?Effective Modern C++?,Item3:理解decltype。
c++ ×10
rvalue-reference ×10
c++11 ×5
c++14 ×2
rvalue ×2
destructor ×1
numerical ×1
rvo ×1
templates ×1
xvalue ×1