草庐IT

is_unique

全部标签

c++ - 在带有 std::unique_ptr 的 lambda 中使用 std::bind

//Byconstl-valuereferenceautofunc2=std::bind([](conststd::unique_ptr>&pw)//fine{std::coutsize()>(22,1));//Bynon-constl-valuereferenceautofunc3=std::bind([](std::unique_ptr>&pw)//fine{std::coutsize()>(22,1));//ByValueautofunc4=std::bind([](std::unique_ptr>pw)//error{std::coutsize()>(22,1));func4(

c++ - 如何为 std::unique_ptr 创建一个有效的 C++ 别名模板

我想为std::unique_ptr创建一个别名模板来提供我自己的删除函数。unique_ptr有一个标量和一个数组实现,它们是这样定义的:template>classunique_ptr//scalartemplateclassunique_ptr//array我在尝试覆盖unique_ptr的标量和数组版本时遇到了麻烦。只为一个版本创建别名很容易,如下所示:templatestructDeleter{voidoperator()(T*ptr){deleteptr;}};templateusingmy_unique_ptr=std::unique_ptr>;但是当我尝试添加第二个别名

c++ - 多态 unique_ptr 复制省略

我有以下代码适用于Clang5.0,但不适用于启用了C++14的Clang3.8:classBase{};classDerived:publicBase{};std::unique_ptrMakeDerived(){autoderived=std::make_unique();returnderived;}intmain(){autobase=MakeDerived();std::coutLiveSampleHere在这种情况下,由于复制省略,返回值在技术上是移动构建的吗?如果是这样,这是否意味着unique_ptr的移动构造函数旨在支持用户类类型的隐式向上转换?从cppreferen

c++ - 为什么 Google Test/Mock 通过 std::unique_ptr 显示泄露的模拟对象错误?

假设有一个Bar对象,它使用了一个Foo对象。所有权是独占的,因此Bar在其构造函数中将Foo作为std::unique_ptr获取。我想用Google测试框架测试Bar,所以我编写了以下代码:usingnamespacetesting;classFoo{public:virtualintF()=0;};classBar{public:Bar(std::unique_ptr&&foo):m_foo(std::move(foo)){}intB(){returnm_foo->F();}private:std::unique_ptrm_foo;};classMockFoo:publicFoo

C++ 单例 : how good is this solution? 优点/缺点,替代方案

我正在开发一个包含多个类的C++项目,这些类必须是单例,它们之间存在依赖关系(初始化顺序很重要)。我想出了这个解决方案:所有我想成为单例的类都有protected构造函数,例如:classMySingleton1{protected:MySingleton1();}有一个源文件singleton_factory.cpp包含一个实例化类Singletons,它派生自所有我想成为单例的类,像这样:#include"MySingleton1.hpp"#include"MySingleton2.hpp"classSingletons:publicMySingleton1,publicMySin

C++ 动态分配不匹配 : Is this problematic?

我被指派处理MFC中的一些遗留C++代码。我在各处发现的其中一件事是如下分配:structPoint{floatx,y,z;};...voidsomeFunc(void){intnumPoints=...;Point*pArray=(Point*)newBYTE[numPoints*sizeof(Point)];...//dosomestuffwithpoints...delete[]pArray;}我意识到这段代码在很多层面上都是严重错误的(C风格转换,使用new像malloc,令人困惑,等等)。我还意识到,如果Point定义了一个构造函数,它就不会被调用,而且如果定义了析构函数,d

c++ - 在 switch 语句中使用类类型 : is it better than using typeid operator?

我在下面看到了有关C++标准$6.4.2中switch语句的内容。Switch语句可以带一个条件。Theconditionshallbeofintegraltype,enumerationtype,orofaclasstypeforwhichasingleconversionfunctiontointegralorenumerationtypeexists(12.3).Iftheconditionisofclasstype,theconditionisconvertedbycallingthatconversionfunction,andtheresultoftheconversion

c++ - "integer constant is too large for ‘long’ 求最大质因数时键入"

我正在解决Euler项目3:Description:Theprimefactorsof13195are5,7,13and29.Whatisthelargestprimefactorofthenumber600851475143?这是我生成答案的代码。但是我需要一个整数类型来保存600851475143。当我在Mac上的GCC上编译它时,我得到:integerconstantistoolargefor‘long’type".我预计longlong可以轻松持有这个数字。我也试过让它未签名。为什么我的代码不能保存这么小的数字?我该怎么做才能让它发挥作用?#include#includeusi

c++ - 在这种情况下 unique_ptr 的行为应该是什么?

假设我有以下内容:std::unique_ptrpA;pA(newA);在这个复杂的例子中,pA(newA);的行为应该是怎样的?是吗?据我所知,在MSVC2010中,voidoperator()(T*)const;在new之后立即调用fromdefault_delete立即返回并删除指针。而g++(4.7.0)给了我nomatchforcall(std::unique_ptr)(A*)错误。 最佳答案 代码不应编译。std::unique_ptr不会重载operator()。VisualC++2011DeveloperPrevie

c++ - std::unique_ptr<T> 不完整类型错误

我有templateclassqueue{private:structnode{Tdata;std::unique_ptrnext;//compileerroronincompletetypenode(T&&data_):data(std::move(data_)){}};std::unique_ptrhead;node*tail;public:queue():tail(nullptr){}我在VS10的标记行上遇到编译错误。在这种情况下,我是否应该被允许使用不完整的类型(实例化模板-构造函数-这里以int为例)?有解决方法吗?编辑singlethreadedqueue.h(62):e