move-semantics

    3熱度

    2回答

    期間避免拷貝構造函數我有一個類A: class A { int value1; int value2; std::string text; public: A(int value1, int value2, std::string text) : value1(value1), value2(value2), text(text) { } }

    2熱度

    1回答

    目前,我正在學習C++,並琢磨一下其中之一就是使用std ::移動 //Code might be incorrect since I havent tested it out Xyt::ByteArray* Xyt::ResourceManager::LoadFileToByteArray(std::string Path) { try { std::ifstream FileS

    1熱度

    1回答

    我想我理解堆棧是如何工作的,當變量被移動時會發生什麼,但是我找不到這個問題的答案。讓我解釋一下: 當一個新的作用域被輸入/創建時,會在堆棧頂部獲取一定量的內存。堆棧指針指向這個內存。它表示堆棧的當前大小。當範圍被留下時,通過使堆棧指針返回到前一個位置來釋放內存。 在C++ 11中移動語義或更高版本將一些數據的所有權從一個變量移動到另一個變量。這避免了複製數據,因爲保存數據的內存保持不變。移動之後,

    4熱度

    2回答

    我要的是這樣的方法: fn take<T>(vec: Vec<T>, index: usize) -> Option<T> 但是,我無法找到這樣的方法。我錯過了什麼嗎?或者是否有一個原因實際上是不安全/不能正確完成的。 編輯:這是從​​3210 有不同的問題的目標是一個remove方法並沒有慌亂的越界進入這裏,我找的是消耗的方法(即,而不是返回的結果。) Vec,並返回其中一個元素。上述問題的

    0熱度

    1回答

    我一直在密切關注的建議從來沒有寫return語句中std::move,for example。除了一些邊緣情況,for example。 我相信,以下是其中std::move可能是值得其他簡單的例子 - 我錯過了什麼?但是我不確定爲什麼,並且在未來的C++中會發生什麼變化? #include <iostream> struct A { }; struct C { }; stru

    21熱度

    2回答

    考慮下面的代碼: #include <iostream> #include <vector> using namespace std; class A { public: A(int) { cout << "int" << endl; } A(A&&) { cout << "move" << endl; } A(const A&) { cout << "c

    0熱度

    1回答

    我想學習移動語義和我讀了一招可能比一個副本更快。不過,我看恰恰相反以下瑣碎代碼: for (int i = 0; i < 100000000; ++i) { std::string a("Copy"); std::string b = a; } for (int i = 0; i < 100000000; ++i) { std::string a("Move")

    7熱度

    1回答

    我有以下的C++(11)代碼: #include <mutex> void unlock(std::unique_lock<std::mutex> && ulock) { } int main(void) { std::mutex m; std::unique_lock<std::mutex> ulock(m); unlock(std::move(ul

    1熱度

    2回答

    可以說我有下面的類: #include <vector> class Foo { public: Foo(const std::vector<int> & a, const std::vector<int> & b) : a{ a }, b{ b } {} private: std::vector<int> a, b; }; 但現在我要考慮到在構造函數

    1熱度

    2回答

    在一個Qt相關的代碼我試圖通過以下的方法,以減少一些重複的線: QPainter createPainter(const QColor& color) { QPainter painter(&m_image); painter.setBrush(color); painter.setPen(color); return painter; } 但是QP