我正在尝试移动语义,我想知道如果右值引用超出范围会发生什么。使用以下代码,如果我将std :: move左值移入,则会遇到运行时问题
function(T t) with t = std::move(lvalue) --> SEGFAULT OR double free
Run Code Online (Sandbox Code Playgroud)
但不成
function(T &&t) with t = std::move(lvalue) --> OK
Run Code Online (Sandbox Code Playgroud)
有人知道为什么吗?
另外,如果交换main()中的两个代码块,则会得到不同的运行时错误0_o
// Compile with:
// g++ move_mini.cpp -std=c++11 -o move_mini
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <utility>
using namespace std;
int num_copied;
class T{
public:
T() : a(nullptr), b(nullptr){};
T(const T &t) : a(new string(*t.a)),
b(new string(*t.b)){
num_copied++;
};
T(T &&t){
*this = move(t);
};
T(string s1, string s2){
this->a = …Run Code Online (Sandbox Code Playgroud) 我从这个问题中获取代码并编辑它以通过显式调用其中一个移动构造对象的析构函数来生成段错误:
using namespace std;
struct Foo
{
Foo()
{
s = new char[100];
cout << "Constructor called!" << endl;
}
Foo(const Foo& f) = delete;
Foo(Foo&& f) :
s{f.s}
{
cout << "Move ctor called!" << endl;
f.s = nullptr;
}
~Foo()
{
cout << "Destructor called!" << endl;
cout << "s null? " << (s == nullptr) << endl;
delete[] s; // okay if s is NULL
}
char* s;
};
void work(Foo&& f2)
{
cout << …Run Code Online (Sandbox Code Playgroud) 我有一个类如下:
class C {
public:
C() : ... {}
~C() {}
Member_1 m_1;
// ...
Member_N m_N;
};
Run Code Online (Sandbox Code Playgroud)
显示的两个特殊成员函数是唯一声明的成员函数.
现在,
static_assert(std::is_nothrow_move_assignable<Member_1>::value);
// ...
static_assert(std::is_nothrow_move_assignable<Member_N>::value);
Run Code Online (Sandbox Code Playgroud)
都很满意.然而,
static_assert(std::is_nothrow_move_assignable<C>::value);
Run Code Online (Sandbox Code Playgroud)
断言.如果我删除空的析构函数,它会通过.
析构函数与移动赋值运算符有什么关系?新规则五?
编译器是GCC 4.9.3 -std=c++0x(由于历史原因).
我正在阅读关于C++ 11中的移动语义,现在我正在尝试理解移动构造函数的实现.假设我们有以下类:
struct A {
A(){ }
virtual ~A(){ }
A(const A&&){ }
};
struct B{
int i;
A a;
virtual ~B(){ }
B(const B&& b){
i = b.i;
i = 0;
//How to move a??
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是如何A在B一个体内调用移动构造函数?我会用std::swap,但寻找它我发现了一些描述.参数是左值引用类型,因此它与移动语义无关.该怎么办?
我正在尝试对我的函数中的所有局部变量使用auto.
请使用以下代码:
class obj
{
public:
obj() {};
obj( obj&& o ) = delete;
};
int main()
{
obj test0;
auto test1 = obj();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译代码:
$ g++ --std=c++1z main.cpp
main.cpp: In function ‘int main()’:
main.cpp:13:20: error: use of deleted function ‘obj::obj(obj&&)’
auto test1 = obj();
Run Code Online (Sandbox Code Playgroud)
请注意,定义test0完全没问题,但尝试执行完全相同类型的test1声明是编译器错误.显然应该是编译器错误,但在这种情况下,是否意味着obj无法用auto定义?我正在使用我无法控制的QT对象来解决这个问题.
我还在使用C++ 98格式来声明变量,还是有其他方法可以使用auto?
谢谢!!!
我有一个嵌套的无序映射,它在C++ 11中包含一个自定义对象.它看起来类似于:
std::unordered_map<std::string, std::unordered_map<std::string, CustomClass>>
Run Code Online (Sandbox Code Playgroud)
此自定义类没有移动构造函数,也不能具有隐式构造函数.尝试分配到此结构时出错:
storageStruct[string_1][string_2].function(parameter);
Run Code Online (Sandbox Code Playgroud)
由于它进入模板,实际的错误链解释真的很长,但这是我觉得最适用的那个:
候选构造函数(隐式移动构造函数)不可行:需要1个参数,但提供了0
我不打算移动这个对象,只是调用它的成员函数.我该如何避免这种不必要的举动?
我想完全理解C++ 11中的移动语义.所以我编写了几个类来查看何时调用不同的构造函数:
#include <iostream>
using namespace std;
class A {
public:
A() : a1_(0) {std::cout << "Calling constructor" << std::endl;}
A(A&& other) {
std::cout << "Calling move constructor" << std::endl;
a1_ = other.a1_;
other.a1_ = 0;
}
// Move assignment operator.
A& operator=(A&& other) {
std::cout << "Calling move operator" << std::endl;
if (this != &other) {
a1_ = other.a1_;
other.a1_ = 0;
}
return *this;
}
// Copy constructor.
A(const A& other) {
std::cout << "Calling copy constructor" << …Run Code Online (Sandbox Code Playgroud) 如何在g ++ - 6.2.1中克服/解决此错误
以下代码适用于g ++ - 7.3.0,但升级编译器对我来说不是一个选项.所以我正在寻找一些SFINAE魔法......尝试很少但到目前为止失败了......
class Base {
public:
Base(std::string str) : s(std::make_shared<std::string>(str)) {}
Base(Base &&base) noexcept { s = std::move(base.s); }
Base &operator=(Base &&i_base_actor) noexcept {
s = std::move(i_base_actor.s);
return *this;
}
virtual ~Base() = default;
private:
std::shared_ptr<std::string> s;
};
// Derived
class Derived : public Base {
public:
Derived() :Base("Derived") {}
~Derived() = default;
};
// Derived1
class Derived1 : public Base {
public:
Derived1(int a) :Base("Derived1") {}
~Derived1() = default;
};
Run Code Online (Sandbox Code Playgroud)
包装功能:
template<typename T, …Run Code Online (Sandbox Code Playgroud) c++ move-semantics variadic-templates perfect-forwarding c++17
我有一些信号原型类
#include <map>
#include <string>
#include <functional>
template <class T>
class Signal
{
public:
std::function<T> slot;
};
Run Code Online (Sandbox Code Playgroud)
接下来是模板单例SignalCollection类,该类自动为Signal
template <class T>
class SignalCollection
{
private:
SignalCollection() {}
SignalCollection(const SignalCollection&) = delete;
SignalCollection& operator= (const SignalCollection&) = delete;
public:
static SignalCollection& Instance()
{
static SignalCollection br{};
return br;
}
std::map<std::string, T> signals_map;
void add(T&& signal)
{
this->signals_map.insert(std::make_pair("a", std::forward<T>(signal)));
}
};
Run Code Online (Sandbox Code Playgroud)
最后,我有一个函数可以推断SignalCollection某些类型Signal
template<class T>
auto& get_collection_for_signal(T&& t)
{
return SignalCollection<T>::Instance();
}
Run Code Online (Sandbox Code Playgroud)
问题是,我无法将值添加到集合图。这是主要的:
void foo()
{
} …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
let display_value = entry.path().display();
files_and_dirs.push(DiskEntry {
path: display_value.to_string(),
is_dir: is_dir(display_value.to_string()),
name: display_value.to_string()
});
Run Code Online (Sandbox Code Playgroud)
如果我这样写:
let display_value = entry.path().display();
let dir_name = display_value.to_string();
files_and_dirs.push(DiskEntry {
path: dir_name,
is_dir: is_dir(dir_name),
name: dir_name
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
之所以发生移动是因为
dir_name具有typestd::string::String,它没有实现Copy特征
我了解在Rust中,赋值时会四处移动。我想声明一个变量,并在第二个代码块中多次使用它。我该怎么做呢?
move-semantics ×10
c++ ×9
c++11 ×7
c++17 ×2
destructor ×1
noexcept ×1
operators ×1
rust ×1
templates ×1
unique-ptr ×1