我已经搜索但找不到"何时"使用它们的答案.我只是听说它很好,因为它为我节省了额外的副本.我把它放在我所拥有的每一堂课中,但有些课程似乎对某些课程没有意义:我读过无数关于LValues和RValues以及std :: move vs. std :: copy vs. memcpy的教程与memmove等等.甚至读取throw(),但我不知道何时使用它.
我的代码看起来像:
struct Point
{
int X, Y;
Point();
Point(int x, int y);
~Point();
//All my other operators here..
};
Run Code Online (Sandbox Code Playgroud)
然后我有一个类数组(RAII sorta thing):
class PA
{
private:
std::vector<Point> PointsList;
public:
PA();
//Variadic Template constructor here..
~PA();
//Operators here..
};
Run Code Online (Sandbox Code Playgroud)
我应该使用移动构造函数和复制构造函数吗?我在Point Class中有它,但感觉很奇怪,所以我把它删除了.然后我在PA课上有它,但我认为它不会做任何事情所以我也删除了它.然后在我的bitmaps类中,我的编译器抱怨有指针成员但没有重载,所以我做了:
//Copy Con:
BMPS::BMPS(const BMPS& Bmp) : Bytes(((Bmp.width * Bmp.height) != 0) ? new RGB[Bmp.width * Bmp.height] : nullptr), width(Bmp.width), height(Bmp.height), size(Bmp.size), DC(0), Image(0)
{
std::copy(Bmp.Bytes, Bmp.Bytes + (width * height), Bytes); …Run Code Online (Sandbox Code Playgroud) 当我尝试编译以下代码时,我得到编译器错误C2248:
#include <list>
#include <memory>
using namespace std;
class data
{
public:
static data parse()
{
data d;
data::parse(d);
return d;
}
list<std::unique_ptr<data>> l;
private:
static void parse(data& node)
{ }
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?我怎样才能解决这个问题?
注意:我没有问题std::shared_ptr而不是使用std::unique_ptr.
请考虑以下代码:
#include <vector>
#include <boost/noncopyable.hpp>
struct A : private boost::noncopyable
{
A(int num, const std::string& name)
: num(num),
name(name)
{
}
A(A&& other)
: num(other.num),
name(std::move(other.name))
{
}
int num;
std::string name;
};
std::vector<A> getVec()
{
std::vector<A> vec;
vec.emplace_back(A(3, "foo"));
// vec.emplace_back(3, "foo"); not available yet in VS10?
return vec; // error, copy ctor inaccessible
}
int main ( int argc, char* argv[] )
{
// should call std::vector::vector(std::vector&& other)
std::vector<A> vec = getVec();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这并不在VS2010编译,因为很明显A是 …
请考虑以下设置.
基类:
class Thing {
int f1;
int f2;
Thing(NO_INIT) {}
Thing(int n1 = 0, int n2 = 0): f1(n1),f2(n2) {}
virtual ~Thing() {}
virtual void doAction1() {}
virtual const char* type_name() { return "Thing"; }
}
Run Code Online (Sandbox Code Playgroud)
和派生类只有通过上述方法的实现才有所不同:
class Summator {
Summator(NO_INIT):Thing(NO_INIT) {}
virtual void doAction1() override { f1 += f2; }
virtual const char* type_name() override { return "Summator"; }
}
class Substractor {
Substractor(NO_INIT):Thing(NO_INIT) {}
virtual void doAction1() override { f1 -= f2; } …Run Code Online (Sandbox Code Playgroud) 我知道这个问题可能看起来非常基本.但我无法找到任何一个没有指针的移动构造函数的例子.
我有一个包含矢量对象变量的类.不是一个指针.所以我的问题:这是否意味着我不需要移动构造函数?或者我的实现是错误的,我应该使用指向向量的指针,然后使用移动构造函数?
谢谢
我是否理解 C++14 标准库使用移动语义?换句话说,我是否可以确信我在以下程序中使用的是移动而不是副本:
#include <iostream>
#include <string>
#include <vector>
using namespace std::string_literals;
std::vector<std::string> greeting()
{
std::vector<std::string> vs {"hello"s, "world"s};
return vs;
}
int main()
{
std::vector<std::string> s = greeting();
std::cout << s[0] << " " << s[1] << "\n" ;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以检查吗?
在下面的例子中怎么样:
#include <iostream>
#include <string>
#include <vector>
using namespace std::string_literals;
class Greeting {
public:
std::string first, second;
Greeting() { first = "hello"s ; second = "world"s ;};
};
Greeting greetingc()
{
Greeting g;
return g;
}
int main() …Run Code Online (Sandbox Code Playgroud) 制作仅移动类型的地图向量似乎在 Windows 上无法正常工作。请参阅此处的代码: https: //godbolt.org/z/yAHmzh
#include <vector>
#include <map>
#include <memory>
// vector<vector<move-only>> works
void foo() {
std::vector<std::vector<std::unique_ptr<int>>> outer;
std::vector<std::unique_ptr<int>> inner;
std::unique_ptr<int> p = std::make_unique<int>(1);
inner.push_back(std::move(p));
outer.push_back(std::move(inner));
}
// vector<map<move-only>> fails to compile upon inserting an element.
void bar() {
std::vector<std::map<std::unique_ptr<int>, std::unique_ptr<int>>> vec;
std::map<std::unique_ptr<int>, std::unique_ptr<int>> map;
std::unique_ptr<int> p1 = std::make_unique<int>(1);
std::unique_ptr<int> p2 = std::make_unique<int>(2);
map.insert(std::make_pair(std::move(p1), std::move(p2)));
// The following line fails to compile on windows. It errors with a message about
// the unique_ptr copy constructor being explicitly deleted. This …Run Code Online (Sandbox Code Playgroud) 我正在阅读Nicolai M. Josuttis的"The C++标准库(第二版)"并刚刚进入了该部分std::pair.作者指出:
从C++ 11开始,
pair<>使用只有非常量复制构造函数的类型将不再编译.
然后他继续给出以下示例:
class A
{
public:
...
A( A& ); // copy constructor with nonconstant reference
...
};
std::pair<A, int> p; // Error since C++11
Run Code Online (Sandbox Code Playgroud)
但是,我对标准委员会决定对标准库标准进行修改的原因感兴趣吗?我试图谷歌的原因,但没有找到任何相关的东西.
当我阅读关于移动语义和右值引用的示例时,它们正在利用rvalue引用的优点并在围绕指针的 大对象周围移动语义,例如:1 2
例如,它们只是复制移动对象内的指针并将其设置为nullptr.(移动/交换)
我的问题是,移动语义对于没有指针但是它们很大的对象有任何优势(性能)吗?
class BigClass
{
int data[BIG_SIZE];
int a01;
.
.
. many members
.
.
int z99;
public:
move constructor ?!
};
Run Code Online (Sandbox Code Playgroud) 我在 std::vector::push_back() 实现中发现了这个:
void push_back(_Ty&& _Val)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
这在 std::map operator[] 实现中:
mapped_type& operator[](key_type&& _Keyval)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
为什么 _Val 和 _Keyval 是逐个引用的?逐个引用的论证是如何工作的?与参考文献相比,这种方法有什么好处?
PS 这不是逻辑“与”,我清楚地理解这一点。
c++ ×10
c++11 ×7
vector ×2
visual-c++ ×2
c++14 ×1
map ×1
new-operator ×1
reference ×1
stl ×1
unique-ptr ×1