我正在做一些移动语义的测试,我试过这个:
class A{
public:
A(){printf("A CTOR\n");}
A(A&) {printf("A CTOR by copy\n");}
A(A&&){printf("A CTOR by universal reverence\n");}
};
A&& create(){
A v;
return std::move(v);
}
auto x{ create() };//this does not compile
float d[]{1.2,1.3,5,6};//this does compile
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error C3086: cannot find 'std::initializer_list': you need to #include <initializer_list>
Run Code Online (Sandbox Code Playgroud)
我不明白,初始化列表功能已添加到VC11与CTP2012 nov.这是因为我们必须等待stdlib的更新吗?
我认为代码是正确的,因为我从Scott meyers的幻灯片中复制它:Move Semantics,Rvalue References,Perfect Forwarding.
谢谢您的帮助. 为了您的信息,发生了虚假副本,因为我没有在我的CTOR中添加"const"副本. 最好
最近我一直在研究C++ 11中的移动语义.我印象非常深刻,以至于我迫不及待地试着把它弄脏了.以下是我的代码:
#include <iostream>
using namespace std;
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper ()
: _p_vals( new int[ 64 ] )
, _size( 64 )
{
cout << "Default constructor: " << this << endl;
}
explicit ArrayWrapper (int n)
: _p_vals( new int[ n ] )
, _size( n )
{
cout << "Constructor: " << this << endl;
}
// move constructor
ArrayWrapper (ArrayWrapper&& other)
: _p_vals( other._p_vals )
, _size( …Run Code Online (Sandbox Code Playgroud) 这个问题是在c ++ 11的背景下.
当从函数返回在堆栈上分配的变量时,如果对象定义了移动构造函数,是否优化了副本?
我有这段代码,移动构造函数和我上面描述的函数:
MyBigObject(MyBigObject&& other) {
// code elided
}
MyBigObject createTheObject() {
MyBigObject emptyObj;
// initialize obj with stuff
return emptyOBj;
}
Run Code Online (Sandbox Code Playgroud)
移动构造函数的这个定义是否意味着所有按值返回都会自动转换为"移动"?或者副本是否会被移动以外的其他机制优化?
如果没有定义移动构造函数,这段代码将如何表现?在那种情况下,完整的副本会发生吗?
在工作的时候,我遇到了一段奇怪/令人困惑的代码,我觉得这些代码与匿名 对象生命周期概念有关.以下是示例代码:
#include<iostream>
#include<string>
class A {
private:
int i;
std::string s;
public:
A(int ii, std::string ss = "Hello") { i = ii; s = ss; }
void Display() { std::cout<<i<<"\n"; }
~A() { std::cout<<"A::~A()"<<"\n";}
};
void function()
{
A a = 1;
//A a = A(1);
a.Display();
}
int main()
{
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
VS2010中的输出1(如果A a = 1)
1
A::~A()
Run Code Online (Sandbox Code Playgroud)
VS2010中的输出2(如果A a = A(1))
A::~A()
1
A::~A()
Run Code Online (Sandbox Code Playgroud)
当析构函数被调用两次(包括匿名)对象时,output2完全有意义.
但是输出1让我困惑,无法理解为什么析构函数 …
来自更多的Java背景我想知道如何通过引用返回一个新对象.我希望Fraction该类是不可变的,以便每个操作都返回一个带有操作结果的新对象.
这将是我在Java中的代码:
class Fraction {
int numerator;
int denominator;
// Constructor
Fraction multiply(Fraction other) {
return new Fraction(other.numerator * this.numerator,
other.denominator * this.denominator);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在C++中这样做,我会创建内存泄漏,因为我无法清理初始化Fraction对象,因为我只有对象的引用.那么,我如何在C++中处理这个问题呢?
class Fraction {
private:
int32_t numerator;
int32_t denominator;
public:
Fraction(int32_t numerator, int32_t denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
Fraction& operator*(Fraction& other) {
auto numerator = other.numerator * this->numerator;
auto denominator = other.denominator * this.denominator;
return *(new Fraction(numerator, denominator))
}
};
Run Code Online (Sandbox Code Playgroud) 我有二维2D阵列(3D坐标),我想从中创建一个3D点矢量.当然,直接的方法是简单的循环,但是使用stl算法可能会更优雅的解决方案存在吗?这是我得到的:
#include <algorithm>
#include <iterator>
#include <vector>
struct point_3d
{
/**
* Default constructor -- set everything to 0
*/
point_3d() :
x(0.0), y(0.0), z(0.0)
{}
/**
* To define 3D point from array of doubles
*/
point_3d(const double crd[]) :
x(crd[0]),
y(crd[1]),
z(crd[2])
{}
/**
* To define 3D point from 3 coordinates
*/
point_3d(const double &_x, const double &_y, const double &_z) :
x(_x), y(_y), z(_z)
{}
double x, y, z;
}; //struct point_3d
//Right-angle tetrahedron
const int …Run Code Online (Sandbox Code Playgroud) 我正在玩这个例子来理解右值引用:
#include <string>
#include <iostream>
#include <utility>
#include <vector>
class Dog
{
public:
Dog() {};
Dog(Dog&& a) { std::cout << "R value" << std::endl;}
};
Dog foo()
{
return Dog();
}
int main()
{
std::vector<Dog> v;
v.push_back(Dog()); // calls move constructor
Dog c((Dog())); // does not call move constructor
Dog d(foo()); // does not call move constructor
}
Run Code Online (Sandbox Code Playgroud)
我很难理解为什么在行v.push_back(Dog())中,对象Dog()被视为Rvalue(因此调用移动构造函数),但以下两行不会调用移动构造函数.我想我可能会误解匿名对象和RValue之间的关系.
编辑:它不是重复,因为这个问题询问编译器在O0中的决定.
据说这里是名称返回值优化(NRVO)是一个优化许多编译器的支持.但它是必须的还是只是一个很好的优化?
我的情况是,我想编译-O0(即没有优化),以方便调试,但我也希望NRVO启用返回对象(例如,向量)的返回语句.如果NRVO不是必须的,编译器可能不会在-O0模式下执行.在这种情况下,我应该更喜欢这个代码:
std::vector<int> foo() {
std::vector<int> v(100000,1); // an object that is really big..
return std::move(v); // explicitly move
}
Run Code Online (Sandbox Code Playgroud)
在这下面?
std::vector<int> foo() {
std::vector<int> v(100000,1);
return v; // copy or move?
}
Run Code Online (Sandbox Code Playgroud)
编辑:我使用的编译器是GCC6,但我希望代码与编译器无关.
在 java 中,可以使用return new int[] {1,2,3}. 在 C++ 中,我习惯先创建一个数组(比如 temp),然后将1,2,3其存储在其中,然后使用return temp. 我如何在{1,2,3}不使用 temp 的情况下返回。我可以在没有临时变量的情况下以类似的方式返回向量或映射(而不是数组)以将它们存储在 C++ 中吗?如果是,我该怎么做?
我在下面有这个私有成员函数,(类模板的一部分,Heap):
template <typename Task, typename Priority>
const size_t& Heap<Task, Priority>::parent_of(const size_t& index) const
{
// ** warning C4172: returning address of local variable or temporary
return (this_index-1)/2;
}
Run Code Online (Sandbox Code Playgroud)
我从其他函数调用它,如下所示:
template <typename Task, typename Priority>
void Heap<Task, Priority>::bubble_up(const size_t& start_index)
{
....
if (priority_of(start_index) > priority_of((parent_of(start_index))))
{
... do work ...
//call recursively the bubble_up
bubble_up(parent_of(start_index));
}
...
}
Run Code Online (Sandbox Code Playgroud)
问题是,priority_of函数参数index在递归的第二次调用中被破坏或释放:
template <typename Task, typename Priority>
const Priority& Heap<Task, Priority>::priority_of(const size_t& index) const
{
return vec.at(index).second; …Run Code Online (Sandbox Code Playgroud)