草庐IT

unique_index

全部标签

c++ - 多重继承和unique_ptr销毁

我有经典的(可能有问题的)多重继承菱形方案。B继承了AC继承了AD继承了C和B我想要一个std::vector可以包含C或D对象,所以我将其设为std::vector这是D爸爸和它工作正常。但是当我使用:std::vector>然后我在破坏vector时出现段错误。**glibcdetected***./a.out:free():invalidpointer:0x0000000009948018***为什么会有差异?对我来说,即使是第一次实现也是有问题的。代码#include#include#includeclassA{public:A()=default;};classB:publi

c++ - 在工厂中返回 unique_ptr

也许这是一个简单的问题,因为我对C++还是个新手。我想使用某种工厂来封装我的应用程序中的日志记录。这个想法是只有工厂知道哪个具体类将处理函数调用。应用程序将始终调用基本日志记录类的抽象接口(interface)。工厂方法应该是这样的:std::unique_ptrFactory::getDefaultLogger(conststd::string¶m){returnnewConcreteLoggingClass(param);}ConcreteLoggingClass是AbstractLoggingClass的子类。但是我得到以下错误:Error:couldnotconvert

c++ - 是否保证 std::unique_ptr 删除顺序?

我正在制作一个控制我的应用程序的全局单例,我希望子系统以特定顺序启动和关闭。classApp{public:App();~App();voidstart();voidrun();voidshutdown();private:std::unique_ptrdisplayManager;std::unique_ptrrenderer;};构造函数以正确的顺序创建指针App::App(){displayManager=std::unique_ptr(newDisplayManager);renderer=std::unique_ptr(newRenderer);}并且我希望以相反的顺序释放u

github页面挑选读数,而不是index.html

我正在使用GitHub页面托管我一直在研究的单个页面应用程序。这是我第一次使用github页面。该项目基于AngularJS,项目的结构就像项目:srcindex.html......这是一个公共回购,您可以看到https://github.com/chanikyamohan/angular-play问题是github页面正在拾取readme文件,而不是index.html。关于如何修复它有什么建议吗?谢谢。看答案您的index.html在SRC文件夹中。将其放在GH页面的顶部

c++ - unique_ptr 的取消引用运算符在 Eclipse 中不起作用

按照thispost中的步骤操作后我设法让Eclipse(Indigo)识别unique_ptr(和其他C++11新东西)。问题是operator->forunique_ptr似乎在Eclipse中不受支持。这里有一个例子:classFoo{voidbar(){/*...*/}};std::unique_ptrfoo;(*foo).bar();//1foo->bar();//2Case1按预期工作:没有错误并且自动完成工作。然而,对于案例2,Eclipse将语句标记为错误(“无法解析方法'bar'”),而且foo->的自动完成功能不起作用。最有趣的是,我对std::shared_ptr

c++11/14 make_unique std::string 的模糊重载

有人可以解释一下如何解决make_unique的模棱两可的过载警告,错误来自何处以及它的确切含义(我确实理解模棱两可的过载是什么,但我不确定为什么我会为这个特定的代码得到一个)?我使用的是c++11,因此我使用了HerbSutter推荐的模板。使用它我得到以下错误:Error4errorC2668:'make_unique':ambiguouscalltooverloadedfunction在visualstudio13中将鼠标悬停在工具提示上给我以下方法:functiontemplate"std::enable_if::value,std::unique_ptr>>::typestd

c++ - 将 unique_ptr 返回到多态类型

我试图通过从我的一些方法返回unique_ptr而不是原始指针来变得更安全。但是,在返回指向多态类型的唯一指针时,我有点困惑。我们如何返回指向派生类类型的基类类型的唯一指针?另外,作为一个不太重要的次要问题-我是否使用移动构造函数正确地从基类创建派生类?这是我的最小示例://StandardIncludes#include#include#include#include//--------------------------------------------------------------------------------------------------classBaseR

c++ - unique_ptr 实现中可能存在的错误

这个问题在这里已经有了答案:Unique_ptrandforwarddeclaration(2个答案)关闭6年前。我试图将unique_ptr类成员与前向声明一起使用。正如许多消息来源所说,例如Forwarddeclarationwithunique_ptr?声明非内联析构函数应该就足够了,但在VS2013和GCC5.3.1中似乎并非如此。我没有测试其他编译器。例子:#includeclassB;classA{public://A();~A();private:std::unique_ptrb;};//classB{};intmain(){Aa;}我只能在取消注释ctor声明或类B声明

c++ - 在什么情况下 std::unique_ptr::operator[] 可能抛出?

我的类有一个operator[],它所做的就是在unique_ptr上调用std::unique_ptr::operator[]>成员(member)。相关部分就是这样:templatestructFoo{T&operator[](constsize_tpos)constnoexcept{returndata_[pos];}std::unique_ptrdata_;};我已将运算符标记为noexcept。但是,unique_ptr::operator[]不是noexcept。我无法找出原因,也不知道我是否可以假设它永远不会抛出。unique_ptr::operator[]本身没有在文档

c++ - 为什么这个 get_index 实现在 VS2017 上失败了?

巴里给了我们thisgorgeousget_indexforvariants:templatestructtag{};templatestructget_index;templatestructget_index>:std::integral_constant...>(tag()).index()>{};按如下方式使用:usingV=variant;constexprconstsize_tN=get_index::value;//1它在Clang(OSX)中运行良好。但在VisualStudio2017中I'mgetting以下内容:(10):errorC2039:'index':is