move-semantics

    0熱度

    1回答

    我試圖實現一個平衡樹,並寫了一個函數,其左旋轉來代替樹: struct BST<'a> { l: Option<&'a BST<'a>>, r: Option<&'a BST<'a>> } impl<'a> BST<'a> { fn left_rotate(self) -> BST<'a> { /* * (x) (y)

    5熱度

    2回答

    我正在處理我的第一個C++項目,它是一個CSV解析器(full source code here)。它正在工作,現在我想要進行基本的重構/提高性能。 目前解析器的工作方式是通過返回每一行作爲std::vector<std::string>,我認爲,而不是分配一個新的向量和一個新的字符串,每次我只有一個內部向量和內部字符串保留內存我會一再清除。 這工作,我開始尋找其他地方,我可能會做內存分配,我看到

    1熱度

    4回答

    我有一個關於在C++中使用std::move的問題。 比方說,我有下面的類,在其構造函數採用std::vector作爲參數: class A { public: A(std::vector<char> v): v(v) {} private: std::vector<char> v; }; 但如果我寫的地方如下: std::vector<char> v; A a(

    4熱度

    1回答

    爲什麼在下面的代碼中沒有編譯錯誤(@)?我認爲lamb是一個左值,所以它不會被綁定到右值引用。 using FunctionType = std::function<void()>; using IntType = int; struct Foo { void bar(FunctionType&&) {} void baz(IntType&&) {} }; Foo f

    7熱度

    2回答

    考慮下面的程序: struct list_wrapper { std::vector<int> m_list; }; int main() { std::vector<int> myList { 1, 1, 2, 3, 5 }; const std::vector<int>::iterator iter = myList.begin(); li

    1熱度

    1回答

    我試圖爲嵌入式設備編譯下面的代碼(它是TI提供的具有C++ 11(C++ 0)實驗支持的交叉編譯器)。目標: 臂阿拉戈-Linux的gnueabi線程模型:POSIX gcc版本4.5.3 20110311 (搶鮮)(GCC) 的移動構造函數的默認符和移動assignement操作員無法編譯(/home/user/test/main.cpp:40:26: error: 'th& th::opera

    0熱度

    1回答

    我有一個C++框架,我提供給我的用戶,他們應該使用我用自己的實現編寫的模板包裝作爲模板類型。 包裝器充當RAII類,它包含一個指向用戶類實現的指針。 爲了使用戶的代碼乾淨整潔(在我看來),我提供了一個轉換操作符,它將包裝器轉換爲它所包含的指針。這種方式(以及其他一些重載)用戶可以使用我的包裝,就好像它是一個指針(很像shared_ptr)。 我遇到了一個角落的情況,用戶調用一個函數,該函數使用指向

    2熱度

    3回答

    我有以下代碼: #include <iostream> #include <memory> #include <vector> class Test { public: Test() {} ~Test() { std::cerr << "Delete\n"; } }; std::vector<std::shared_ptr<Test>> makeList()

    11熱度

    3回答

    鑑於 std::list<std::vector<float>> foo; std::vector<float> bar; 我應該如何移動bar到foo結束而不復制數據? 可以麼? foo.emplace_back(std::move(bar));

    3熱度

    2回答

    我做了一個模板類derived_object<T>,它與std::unique_ptr非常相似。它具有附加功能,可以進行深層複製,但T只是實際對象的基類。該技術取自boost::any。下面是該文件derived_object.h的(可惜長)內容: #pragma once #include <type_traits> #include <algorithm> template<class