move-semantics

    1熱度

    1回答

    有沒有寫一個拷貝構造一類(比方說,Copyable,即持有std::unique_ptr到Base類(但一個真正的方式存儲Derived對象。 快速測試顯示了預期的切片時,因爲Copyable不知道它的控股真正的類型,所以我想需要一個clone方法,但我不知道是否有辦法讓編譯器在一些更好的方式來處理這個 縱切代碼:? #include <algorithm> #include <iostream

    1熱度

    2回答

    所以我在下面的文件,其內容如下: 啊: #ifndef A_H #define A_H class A { }; #endif BH: #ifndef B_H #define B_H #include <memory> class A; class B { public: B(std::unique_ptr<A> a); private: st

    -1熱度

    1回答

    我最近看了這個std::thread參考。 對於移動構造函數,它說: thread(thread&& other); 移動的構造。構造線程對象來表示由 其他表示的執行線程。 此次調用後其他不再代表 執行的線程。 此外,在下面的例子中有這些行: int n=0; std::thread t3(f2, n); std::thread t4(std::move(t3)); // t4 is no

    1熱度

    2回答

    這是我的理解,當一個基本的數據類型被移出時,它將始終執行一個副本。 例如,你永遠無法從int移動,並有原始int沒有指定值: #include <iostream> int main() { int x = 100; int y{std::move(x)}; std::cout << x << " " << y; //always prints "100 100

    1熱度

    1回答

    是否有一種乾淨的方式來移動一套集合中的元素?例如: set<A> s {...}; vector<A> v; for (const A& a : s) v.push_back(move(const_cast<A&>(a))); s.clear(); //undefined behavior?

    5熱度

    1回答

    在閱讀關於C++ 11時,我有一種感覺,當使用標準容器(如std::vector)和用戶定義的數據類型時,我們鼓勵提供noexcept移動操作(如果有的話),因爲隨後 - 只 - 然後容器會內部真正移動數據而不是複製。 當今天嘗試了這個,我發現與-std=c++1y(對於C++ 14)和g ++ - 4.8甚至沒有區別。也許我錯過了規範中的更新,也許我的例子是錯誤的。 我比較了三種數據結構,應該是

    3熱度

    2回答

    我想構建一個可調用的對象鏈,以後可以異步執行。我想嘗試以下方法:構建節點的「嵌套」結構(通過將每個節點移動到其「父」中)導致存儲所有計算的對象,並且可以按需啓動鏈。 這是我腦子裏想的: template <typename TParent, typename TF> struct node { TParent _parent; TF _f; node(TPare

    3熱度

    4回答

    假設驗證碼: class Parent {} class Child : public Parent {} static std::vector<std::unique_ptr<Child>> Foo(); 有沒有寫這個功能的一個簡單的方法: std::vector<std::unique_ptr<Parent>> Bar() { auto children = Foo();

    7熱度

    3回答

    假設有這樣的功能,移動: int * func() { std::unique_ptr<int> ptr(new int(3)); //Few more lines of code //then one function added where programmer writes like some thing SOME_OTHER_FUNC(std:

    4熱度

    3回答

    在我的API中,我有一個返回std::istringstream的函數。 std::istringstream class是不可複製的,但支持移動一個符合標準的編譯器,返回本地std::istringstream沒有問題。 但是,在gcc 4.9上,有no support用於移動std::istringstream。 有沒有一些解決方法,我可以使用該std::istringstream而不從用戶的