ICode9

精准搜索请尝试: 精确搜索
  • c-无法将带有NULL删除器的std :: unique_ptr移动到std :: shared_ptr?2019-10-11 14:08:16

    我想将NULL std :: unique_ptr移至std :: shared_ptr,如下所示: std::unique_ptr<float> test = nullptr; std::shared_ptr<float> test2 = std::move(test); 据我所知,这样做是合法的,并且在Visual Studio 2015和GCC中运行良好. 但是,我不能对具有删除器声明的std :: unique_ptr

  • c-如何在Mac OS X上使用gdb打印出vector中的内容2019-10-11 02:07:30

    我试图在Mac OS X上使用C 11功能.我下载了带有端口的g 4.8.1. 这是测试代码. 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(1

  • c – 使用std :: make_unique的push_back或emplace_back2019-10-06 14:07:58

    根据these questions here中的答案,我知道使用c 14的std :: make_unique肯定比使用emplace_back(new X)更直接. 那就是说,打电话是首选 my_vector.push_back(std::make_unique<Foo>("constructor", "args")); 要么 my_vector.emplace_back(std::make_unique<Foo>("con

  • c – 使用std :: unique_ptr的双(二维)数组2019-10-05 06:17:00

    我有一个指针分配给指针的双数组. // pointer to pointer int **x = new int *[5]; // allocation for (i=0; i<5; i++){ x[i] = new int[2]; } for (i=0; i<5; i++){ // assignment for (j=0; j<2; j++){ x[i][j] = i+j; } }

  • c – 为了支持移动语义,函数参数应该由unique_ptr,value还是rvalue获取?2019-09-30 10:05:49

    我的一个函数将vector作为参数并将其存储为成员变量.我正在使用const引用,如下所述. class Test { public: void someFunction(const std::vector<string>& items) { m_items = items; } private: std::vector<string> m_items; }; 但是,有时项目包含大量字符串,因此

  • c – unique_ptr到派生类,作为将unique_ptr带到基类的函数的参数2019-09-30 05:06:27

    我试图在一个函数中使用unique_ptr派生类,该函数将unique_ptr带到基类.就像是: class Base {}; class Derived : public Base {}; void f(unique_ptr<Base> const &base) {} … unique_ptr<Derived> derived = unique_ptr<Derived>(new Derived); f(derived); 如果我正确理解t

  • C 11 unique_ptr和shared_ptr是否能够转换为彼此的类型?2019-09-30 04:06:07

    C 11标准库是否提供了从std :: shared_ptr转换为std :: unique_ptr的任何实用程序,反之亦然?这是安全的操作吗?解决方法: std::unique_ptr is the C++11 way to express exclusive ownership, but one of its most attractive features is that it easily and efficiently conver

  • 如何在DDD(或gdb)中使用unique_ptr调试C 11代码?2019-09-28 17:07:19

    std :: unique_ptr很不错,但是在DDD或gdb调试时我发现它们不太舒服. 我正在使用gcc中的gdb pretty打印机(例如/usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py).这是可读性的一大胜利,例如: $print pTest std::unique_ptr<MyType> containing 0x2cef0a0 但是,取消引用指针

  • c – 带SDL的智能指针2019-09-26 21:08:16

    对于我的游戏,我应该使用原始指针来创建SDL_Window,SDL_Renderer,SDL_Texture等,因为它们具有特定的删除功能 SDL_DestroyTexture(texture); 或者我应该在创建unique_ptr或shared_ptr时添加自定义删除器,如果是这样,我将如何使用SDL类型执行此操作?解决方法:您可以创建一个具有多

  • c – 异常安全和make_unique2019-09-26 06:06:44

    只是为了澄清一下,使用make_unique只会在表达式中有多个分配时增加异常安全性,而不只是一个,对吗?例如 void f(T*); f(new T); 是完全异常安全(就分配和东西而言),而 void f(T*, T*); f(new T, new T); 是不正确的?解决方法:不仅在您有多个分配时,而且每当您可以在不同的地方投

  • c – unique_ptr和OpenSSL的STACK_OF(X509)*2019-09-25 18:06:35

    我使用一些using语句和unique_ptr来使用OpenSSL,如suggested in another question.没有,代码变得非常难看,我不是很喜欢goto语句. 到目前为止,我已尽可能地更改了我的代码.以下是我使用的示例: using BIO_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>; using X509_ptr = std

  • c – 使用new的std :: make_unique和std :: unique_ptr之间的差异2019-09-15 19:05:45

    std :: make_unique是否具有std :: make_shared等效率优势? 与手动构建std :: unique_ptr相比: std::make_unique<int>(1); // vs std::unique_ptr<int>(new int(1)); 解决方法:make_unique背后的动机主要有两个方面: > make_unique对于创建临时对象是安全的,而在明确使用ne

  • c – 使用std :: unique_ptr的双链表2019-09-01 05:16:51

    有人建议实施吗?前几天我在家里试过这个,发现移动语义太难建立先前的链接或简单的链表.使用std :: unique_ptr创建树很容易.当然,由于复制/分配,std :: shared_ptr可以轻松实现这个问题.那怎么样?解决方法:由于这个问题已经重新开放,我会发表评论,因为我认为这是一个答案: 如果你的意

  • c – ‘&’需要l-value on&std :: unique_ptr <>.get2019-09-01 01:17:52

    我正在尝试使用函数NuiCreateSensorByIndex(int,INuiSensor**).我试图不使用裸指针,所以我做了std :: unique_ptr< INuiSensor> NUI;使它成为unique_ptr. 现在我想访问此函数,所以我执行以下操作:hr = NuiCreateSensorByIndex(i,& nui.get());,但这是错误的: KinectManager.cpp:29:

  • c – std :: unique指针和自定义lambda删除器错误2019-08-31 18:08:52

    我试图使用std :: unique_pointer并提供自定义lambda删除器,但我收到语法错误: cannot convert from 'wmain::<lambda_0f8f736f48c52ca6fa24492e7c0c1ec0>' to 'const std::default_delete<_Ty>' 使用以下简单的最小代码: #include <memory> class TestClass { }; t

  • c – 为什么我不能用删除器创建这个unique_ptr?2019-08-31 13:07:00

    我使用SDL并遇到了一个我无法解决的问题.我想在std :: map中保留纹理(指向struct的指针),其中我使用std :: string作为键,std :: unique_ptr< texture,void(*)(texture *)>作为价值.我必须在std :: unique_ptr中使用删除器,因为纹理必须由某个函数释放.纹理也由另一个函数创建.我将

  • c – 为什么不能使用initializer_list来初始化unique_ptr的向量?2019-08-30 12:09:20

    参见英文答案 > initializer_list and move semantics                                    6个 我想知道为什么initializer_list不能与unique_ptr一起使用: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_

  • c – 将原始指针和智能指针的容器传递给模板函数2019-08-29 20:05:17

    当容器(例如:std :: vector)传递给函数模板时,是否有可能从容器中抽象出对象的指针类型? 我有以下两种方法: template <typename T, typename allocT, template <typename, typename> class containerT> static void parse(containerT<T *, allocT> &entries, const rapidjson::Value

  • c – std :: unique_ptr使用自定义删除器,参数很少2019-08-29 14:04:54

    我想知道是否可以使用多个参数(标准删除签名)为std :: unique_ptr指定自定义删除器.我知道使用std :: shared_ptr存在与std :: bind的解决方法,这使得它成为可能但是std :: unique_ptr存在的一些技巧? 对我来说似乎不是因为根据http://en.cppreference.com/w/cpp/memory/unique_ptr:

  • c – 尝试在向量中find_if一个unique_ptr时出现编译错误2019-08-28 20:06:40

    这段代码: #include <memory> #include <vector> #include <algorithm> struct Foo { int bar; Foo(const int val) : bar(val) { } }; int main() { std::vector<std::unique_ptr<Foo>> vec; vec.emplace_bac

  • c – 在函数调用中访问并移动unique_ptr2019-08-24 21:08:30

    我有一个类似于以下的部分. struct derive : base{ derive(unique_ptr ptr): base{func(ptr->some_data), std::move(ptr)}{} }; 从理论上讲,它应该有效.但由于编译器(vs2015)没有严格遵循标准,因此func(ptr-> some_data),std :: move(ptr)的顺序是未定义的,即ptr可以在访问

  • c – 查看std :: unique_ptr及其nullptr_t构造函数2019-08-24 21:05:23

    我试图理解为什么unique_ptr有一个nullptr_t构造函数 constexpr unique_ptr::unique_ptr( nullptr_t ); 我假设这是因为正常的一个参数构造函数是显式的,因此会拒绝nullptr值: explicit unique_ptr::unique_ptr( pointer p ); 但是当我构建一个例子时,编译器很好: namespace Tho

  • 使用std :: unique_ptr的C Pimpl Idiom Incomplete Type2019-08-24 20:05:19

    我为证明问题所需的大量代码道歉.我在使用带有std :: unique_ptr的pimpl习惯用法时遇到了问题.具体地说,当一个类(具有pimpl’ed实现)被用作具有pimpl’ed实现的另一个复合类中的成员数据时,似乎会出现问题. 我能找到的大多数答案都缺少explicit destructor declaration,但正如你在

  • C如何从一个在构造函数上获取参数的类创建一个std :: unique_ptr2019-08-24 15:18:48

    我需要从一个具有一个参数的构造函数的类创建一个std :: unique_ptr.我找不到关于如何做的参考.这是无法编译的代码示例: #include <iostream> #include <string> #include <sstream> #include <memory> class MyClass { public: MyClass(std::string name);

  • c – 使用unique_ptr设计头痛2019-08-24 11:16:22

    假设我们有Foo课: class Foo { public: ... }; Foo有一个实例方法,它可以将Foo实例转换为另一个Foo实例,或者返回相同的Foo实例: <some appropriate pointer type to Foo> Foo::tryToTransform() { if (canBeTransformed()) { <delete this Foo instance>; return <ne

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有