相关疑难解决方法(0)

为什么右值在使用后不会立即销毁?

我编写了以下程序,并期望从中获得的右值std::move()在函数调用中使用后会立即被销毁:

struct A
{
    A(){ }
    A(const A&){ std::cout << "A&" << std::endl; }
    ~A(){ std::cout << "~A()" << std::endl; }
    A operator=(const A&){ std::cout << "operator=" << std::endl; return A();}
};

void foo(const A&&){ std::cout << "foo()" << std::endl; }

int main(){
    const A& a = A();
    foo(std::move(a)); //after evaluation the full-expression 
                       //rvalue should have been destroyed
    std::cout << "before ending the program" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

但事实并非如此。改为产生以下输出:

foo()
before ending the program
~A()
Run Code Online (Sandbox Code Playgroud)

演示

正如答案中所说 …

c++ move-semantics

3
推荐指数
1
解决办法
854
查看次数

在 Qt 中移动语义而不使用指针?

我有一个 Qt 项目,那里有一个对象,它将被复制很多次。因此我想添加移动语义。

#ifndef OBJECTTOCOPY_H
#define OBJECTTOCOPY_H

#include <QColor>
#include <QString>
#include <QDataStream>
namespace level_1     {
namespace level_2 {
class ObjectToCopy {
public:

  explicit ObjectToCopy(const QString& _name = "", const QColor& colorBody = QColor() );

  // MOVE
  ObjectToCopy(ObjectToCopy && other);  

  static quint32 _valueInt32;
  static quint16 _valueInt16;
  QString _name;
  QColor  _colorBody;

private:
};
}
}
#endif // OBJECTTOCOPY_H
Run Code Online (Sandbox Code Playgroud)

由于成员变量不是指针,因此如何窃取它们的指针?

ObjectToCopy::ObjectToCopy (ObjectToCopy&& other)
    : _valueInt32( other._valueInt32  )
    , _valueInt16( other._valueInt16 )
    , _name( other._name )
    , _colorBody( other._colorBody )
{
    other._valueInt32 = …
Run Code Online (Sandbox Code Playgroud)

qt move move-semantics c++11 qt5

3
推荐指数
1
解决办法
5841
查看次数

移动语义如何保留临时变量的数据?

我正在读这篇文章:什么是移动语义?

请注意,移动构造函数的帖子中给出的示例是:

string(string&& that)  
{
    data = that.data;
    that.data = nullptr;
}
Run Code Online (Sandbox Code Playgroud)

当我们string a(x+y)用来构造一个新的字符串时,我发现它很混乱.由于结果x+y是一个临时变量,它很快就会被破坏.这意味着复制指针(data = that.data)确实会复制一个悬空指针,原始数据(应该存储在x+y函数调用完成后清理的堆栈帧中)被销毁.似乎设置that.data为nullptr无济于事,因为堆栈框架无论如何都会被清理干净.

任何人都可以解释为什么这不是一个问题?c ++如何实际处理这种情况?

c++ move-constructor move-semantics

3
推荐指数
1
解决办法
439
查看次数

为什么复制的字符串被复制复制......?

想象一下这个简单的简化代码片段:

ostringstream os; 
os << "hello world!";           

string str = os.str().c_str();  // copy of os

list<string> lst;
lst.push_back(str);             // copy of str
Run Code Online (Sandbox Code Playgroud)

在WideString中有一个名为detach()的函数,它赋予被调用函数处理mem-allocation的责任.

字符串类型有这么类似的技巧吗?

c++

3
推荐指数
1
解决办法
136
查看次数

无法理解 std::move 的实现

这是std::move(). 它不完全符合标准的细节,但非常接近:

template<class T>
typename std::remove_reference<T>::type&&
myMove( T&& Arg )
{   
    return ( ( typename std::remove_reference<T>::type&& )Arg );
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这是行不通的,如果我们更换typename std::remove_reference<T>::type&&T&&,即

template<class T>
typename std::remove_reference<T>::type&&
myMove( T&& Arg )
{   
    return ( (T&&) Arg );
}
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics

3
推荐指数
1
解决办法
1493
查看次数

使用push_back将向量<double>放入向量<vector <double >>

我有一个std::vector<vector<double>>我想填写一个功能.我需要在我的程序中稍后为某些计算存储一些3维坐标.

我的问题是如果我这样做:

//in classdefinition
std::vector<std::vector<double>> data;

    myFunc()
    {
        std::vector<double> temp;
        temp.push_back(1);
        temp.push_back(2);
        temp.push_back(3);
        data.push_back(temp);

        temp.clear();
        //new data
        temp.push_back(2);
        temp.push_back(3);
        temp.push_back(4);
        data.push_back(temp);
    }
Run Code Online (Sandbox Code Playgroud)

清除和重新填充temp会影响数据中的值吗?

我已经找到了这个http://www.cplusplus.com/reference/vector/vector/push_back/, 但由于解释是"将val的内容复制(或移动)到新元素." 我不知道该怎么想.对我来说,这听起来像是一个矛盾.

我认为如果变量作为参考传递是没有多大意义的,因为它们可以像我的情况一样只在有限的范围内有效.我的假设是对的吗?

c++ stdvector

3
推荐指数
1
解决办法
540
查看次数

C++ 编译器如何决定何时为 std::vector 或任何对象调用移动构造函数

我正在阅读 std 模板库书籍,但对 STL 容器章节中列出的以下详细信息感到困惑。显然,它指定了 STD::VECTOR 操作和效果

Operation                     Effect

vector<Elem> c(c2)  | Copy constructor; creates a new vector as a copy of c2 (all elements are copied)
vector<Elem> c = c2 | Copy constructor; creates a new vector as a copy of c2 (all elements are copied)
vector<Elem> c(rv)  | Move constructor; creates a new vector, taking the contents of the rvalue rv (since C++11)
vector<Elem> c = rv | Move constructor; creates a new vector, taking the contents of …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

3
推荐指数
3
解决办法
1325
查看次数

在 C++ 中创建 Promise 向量并收集 future 向量的正确方法是什么?

我正在尝试将循环分配到多个线程中,使用 C++ Promise 和 future 评估并收集主线程中的聚合结果,如下所示:

std::vector<std::promise<std::tuple<uint64_t, uint64_t, uint64_t>>> promiseVector;
std::vector<std::future<std::tuple<uint64_t, uint64_t, uint64_t>>> futureVector;;
std::vector<std::thread *> threadPool;
                
// create threads
for(int ti = 0, initialval = 0; ti<max_num_threads; ti++, initialval += size_per_thread) {
    // create promise
    promiseVector.push_back(new std::promise<std::tuple<uint64_t, uint64_t, uint64_t>>(std::make_tuple(0, 0, 0)));
    
    // create futures
    futureVector.push_back(promiseVector[ti].get_future());
    
    // create thread and start
    threadPool.push_back(
        new std::thread([&]{
            uint64_t rv1 = 0, rv2 = 0, rv3 = 0;
            
            // some threadwise work
            for (int ri = initialval, i = 0; i < size_per_thread; ri …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading

3
推荐指数
1
解决办法
1554
查看次数

std :: wstring指针赋值C++

我试图为wstring指针赋值,并且没有赋值; 但是,当我打破创建并将指针分配到两行时,它可以工作.为什么是这样?

例如:

std::wstring* myString = &(L"my basic sentence" + some_wstring_var + L"\r\n");
Run Code Online (Sandbox Code Playgroud)

以上不起作用,但下面的确有效:

std::wstring temp = (L"my basic sentence" + some_wstring_var + L"\r\n");
std::wstring* myString = &temp;
Run Code Online (Sandbox Code Playgroud)

c++ pointers wstring

2
推荐指数
1
解决办法
3687
查看次数

你能在C++中返回一个字符串对象的局部变量吗?

我在某处发现了以下代码,我想知道它是否合法在C++中.ret变量是堆栈变量,一旦foo返回分配给ret的内存不再存在.但是string是一个类,我认为复制构造函数被调用来将ret的内容复制到var.这是真的?以下是一段有效的代码吗?

    string foo(int x)
    {
        string ret; 
        //some operation on ret

        return ret; 
    }
    string callingFunc()
   {
        string var = foo(2); 
        // some operation on var
   }
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks copy-constructor

2
推荐指数
1
解决办法
93
查看次数