unique-ptr

Cannot iterate over map whose elements hold a uniq_ptr

六月ゝ 毕业季﹏ 提交于 2019-12-12 02:52:46
问题 The following code fails to compile: #include <iostream> #include <memory> #include <map> struct HaveUniq { std::unique_ptr<int> uniq; }; void print_hus(const std::map<int, HaveUniq>& hus) { for (const std::pair<int, HaveUniq>& p: hus) std::cout << *p.second.uniq << std::endl; } int main() { std::map<int, HaveUniq> hus; for (int i = 0; i < 10; ++i) hus[i].uniq = std::unique_ptr<int>(new int(i)); print_hus(hus); } With the following error: uniq_wtf.cpp: In function ‘void print_hus(const std:

Error using Qt and unique pointers

蓝咒 提交于 2019-12-11 20:46:04
问题 I am currently working on an interface for an random application. In the interface I have a QListView , which have three buttons related to it. The first one is add, which adds a new item on the widget, this button works fine. The problem comes with the clear button: void NeuralNetwork::on_clearButton_clicked() { this->model.get()->clear(); this->item.clear(); } as you see both model and item are attributes of this class. They are declared as this: std::unique_ptr<QStandardItemModel> model =

Should I move a unique_pointer or should I send a pointer to a unique_pointer?

喜你入骨 提交于 2019-12-11 20:31:46
问题 I am writing a c++ implementation of a BST tree using std::unique_ptr. I am a hobbyist programmer. Initially, I wrote an insert function using a helper function and passing the pointer using move semantics, but that forced me to return unique_ptr. I then considered passing a pointer to the unique_ptr. I even considered using unique_ptr::get() and sending a pointer to the binaryNode, but I have read that this function should only be used when interfacing with legacy functions (Implementation

lldb on xcode vs lldb on standalone

孤街浪徒 提交于 2019-12-11 18:59:42
问题 I have this C++11 code that uses unique_ptr in vector. #include <vector> #include <iostream> #include <memory> using namespace std; class A { int x; public: A() {} ~A() {} A(A& a) {} A(int x) {this->x = x;} int get() {return x;} }; int main() { vector<unique_ptr<A>> v; auto a = new A(10); unique_ptr<A> pa(a); v.push_back(move(pa)); // move(pa); auto a2 = new A(20); unique_ptr<A> pb(a2); v.push_back(move(pb)); // move(pa); for (auto& i: v) { cout << i->get(); } } I could use Xcode to debug and

Workaround for Making a unique_ptr to a Forward Declared Type

牧云@^-^@ 提交于 2019-12-11 17:58:54
问题 Given that I have a forward declared type: class Foo; I want to make a unique_ptr to this type: unique_ptr<Foo> pFoo; This works fine in visual-studio-2017 but I can't make it work in visual-studio-2012. C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1150): error C2027: use of undefined type Foo (....\src\STETestbed\STETestbed.cpp) O:\Engine\stetestbed\include\STETestbed\ComponentDirector.h(26) : see declaration of Foo C:\Program Files (x86)\Microsoft Visual Studio 11.0

How to return/copy values of an unique_ptr<unsigned char[]>?

家住魔仙堡 提交于 2019-12-11 17:55:06
问题 I have a simple class with one attribute std::unique_ptr<unsigned char[]> in C++. I want to have a function that converts string to std::unique_ptr<unsigned char[]> , other to convert float to std::unique_ptr<unsigned char[]> , and a third to return the attribute std::unique_ptr<unsigned char[]> . My header is compiling but the source CPP is not. Even the return attribute is not compiling. #include <memory> class SkinnyBuffer { private: std::unique_ptr<unsigned char[]> _buff; public:

Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to “.cpp”?

烂漫一生 提交于 2019-12-11 15:22:48
问题 The code would work file as long as I don't move the definition of constructor (of B ) to the header B.h . B.h class Imp; //<--- error here class B{ public: std::unique_ptr<Imp> imp; B(); //<--- move definition to here will compile error ~B(); //// .... other functions .... }; B.cpp #include "B.h" #include "Imp.h" B::B(){ } ~B::B(){ } Imp.h class Imp{}; Main.cpp (compile me) #include "B.h" Error: deletion of pointer to incomplete type Error: use of undefined type 'Imp' C2027 I can somehow

compilation error when using std::unique_ptr as value in tbb::concurrent_hash_map

独自空忆成欢 提交于 2019-12-11 11:22:03
问题 I'm trying to replace a std::unordered_map with a tbb::concurrent_hash_map . My original code: typedef std::unique_ptr<V> V_ptr; std::unordered_map<K, V_ptr> hm; V_ptr v (new V); K k; hm.insert (std::make_pair (k, std::move (v))); compiles fine with clang 3.3. Switching the unordered_map to a concurrent_hash_map: typedef std::unique_ptr<V> V_ptr; tbb::concurrent_hash_map<K, V_ptr> hm; V_ptr v (new V); K k; hm.insert (std::make_pair (k, std::move (v))); results in the error: ...stl_pair.h:105

C++ unique_ptr and arrays

随声附和 提交于 2019-12-11 11:11:35
问题 I'm trying to use arrays with unique_ptr with no success. What is the correct way to declare a unique_ptr of some size? (size is some paramter). unique_ptr<A[]> ptr = make_unique<A[]>(size); Here's an example: #include <iostream> #include <string> #include <vector> #include <functional> #include <memory> using namespace std; class A { string str; public: A(string _str): str(_str) {} string getStr() { return str; } }; int main() { unique_ptr<A[]> ptr = make_unique<A[]>(3); } This is not

unique_ptr as class member and move semantics fail to compile with clang

浪尽此生 提交于 2019-12-11 07:51:15
问题 I can't get clang (Apple LLVM version 4.2 (clang-425.0.28)) to compile these classes: struct A { int f(){return 2;} }; class Cl{ std::unique_ptr<A> ptr; public: Cl(){ptr = std::unique_ptr<A>(new A);} Cl(const Cl& x) : ptr(new A(*x.ptr)) { } Cl(Cl&& x) : ptr(std::move(x.ptr)) { } Cl(std::unique_ptr<A> p) : ptr(std::move(p)) { } void m_ptr(std::unique_ptr<A> p){ ptr = std::unique_ptr<A>(std::move(p)); } double run(){return ptr->f();} }; I would like to run the constructor as follows: std: