unique-ptr

    5熱度

    1回答

    我正在研究一個庫,它有兩個不同的最終用戶,其中一個使用gcc 4.5.3,另一個剛搬到gcc 4.6.3。該庫使用新的C++ 11智能指針(特別是unique_ptr),並在gcc 4.5.3上編譯得很好。但是,在這兩個版本之間,gcc開始支持nullptr,因此unique_ptr的API更改爲與標準更緊密匹配。在現在這樣做下面的代碼從細去曖昧 unique_ptr up(new int(30)

    3熱度

    4回答

    有一個被測試的類,它目前在其構造函數中接受一個unique_ptr<Interface>&&,表示它想要對接口實現進行單一所有權。當想要使用模擬Interface來測試這個類時出現了問題:雖然模擬框架(HippoMocks)只給我提供了我不擁有的Interface*,因此不能刪除。 我測試類以const shared_ptr<Interface>&作爲參數時之前同樣的問題,但固定的,通過提供一個自

    4熱度

    4回答

    我們有一個廣泛的代碼庫,目前使用原始指針,我希望能夠遷移到unique_ptr。但是,許多函數都將raw指針作爲參數,而unique_ptr不能用於這些情況。我意識到我可以使用get()方法來傳遞原始指針,但是這增加了我必須接觸的代碼行數,並且我覺得它有點不雅觀。我滾我自己的unique_ptr看起來像這樣: template <class T> class my_unique_ptr: pub

    0熱度

    2回答

    這是使用的unique_ptr代碼: struct Foo{ Foo(){std::cout << "Ctor called\n";} ~Foo(){std::cout << "Dtor called\n";} void bar(){std::cout << "bar called\n";} } int main(){ unique_ptr<Foo> up(n

    4熱度

    3回答

    我在玩fluent interface模式。 首先,我寫了這樣的事情: class C { public: C() { } C* inParam1(int arg1){ param1 = arg1; return this; } C* inParam2(int arg2){ param2 = arg2; return this; } private:

    3熱度

    2回答

    我來到一個奇怪的段錯誤。原因居然使我的錯誤,但我還是不明白爲什麼一個分割故障導致這裏...代碼是: #include <memory> int main(int argc, char **arv) { int *i = new int; std::unique_ptr<int> u1(i); std::unique_ptr<int> u2; u1 = s

    1熱度

    2回答

    我已經使用-std = C++ 0x選項在gcc 4.4.6中啓用了unique_ptr。它似乎工作得很好,正是我所需要的 - 一個定製刪除器的範圍指針。 但是我確實發現了一個問題。 typedef std::unique_ptr<X> XPtr; XPtr ptr1(new X); XPtr ptr2(new X); std::cout << "ptr1 points to " <<

    12熱度

    3回答

    我試着寫這個類 #include <memory> class ContainerUnique { public: ContainerUnique(void); ~ContainerUnique(void); private: std::unique_ptr<UniqueElement> u; }; 哪裏UniqueElement是別處定義的POD

    0熱度

    1回答

    我試圖將unique_ptr的unordered_map移動到另一個映射中,但在下面得到編譯錯誤。 #include <memory> #include <unordered_map> #include <string> int main() { std::unordered_map<int, std::unique_ptr<int>> aMap; std::unor

    7熱度

    1回答

    struct test_struct { test_struct() {} ~test_struct() {} }; #include <vector> #include <memory> #include <cstdio> int main() { printf("ctor begin\n"); { std::vector<s