草庐IT

SOME_UNIQUE_VALE

全部标签

c++ - 将 std::unique_ptr 与 lambda 交换为删除器——GCC

我们可以使用lambda作为带有std::unique_ptr的删除器吗?实际上,我是用clang++做的,它很高兴这样做。我正在使用std::swap换成std::unique_ptr;其中autodeleter=[](structaddrinfo*ptr){if(ptr!=nullptr){freeaddrinfo(ptr);}};.Clang的交换似乎不需要复制赋值运算符,但gcc的std::swap需要,正如您在这些日志中看到的那样:Infileincludedfrom/usr/include/c++/4.8.1/memory:81:0,from/home/zenol/proj

c++ - 将具有 unique_ptr 参数的函数绑定(bind)到 std::function<void()>

我正在尝试使以下代码工作:#include#include#include#includeusingnamespacestd;classFoo{public:Foo():m_str("foo"){}voidf1(strings1,strings2,unique_ptrp){printf("1:%s%s%s\n",s1.c_str(),s2.c_str(),p->str());}voidf2(strings1,strings2,Foo*p){printf("2:%s%s%s\n",s1.c_str(),s2.c_str(),p->str());}constchar*str()const{

c++ - '从 some_type** 到 const some_type** 的无效转换'

我有一个函数需要constsome_type**作为参数(some_type是一个结构,函数需要一个指向这种类型数组的指针).我声明了一个some_type*类型的局部变量,并对其进行了初始化。然后我将该函数称为f(&some_array),编译器(gcc)说:error:invalidconversionfrom‘some_type**’to‘constsome_type**’这里有什么问题?为什么我不能将变量转换为常量? 最佳答案 参见:Whycan'tIpassachar**toafunctionwhichexpectsaco

c++ - 什么叫表达式 `T (&some(...)) [2]` where T=char

我在库实现中看到过这个表达式,我基本上理解它被用来培养SFINAE甚至拉动static_assert触发器。它基本上采用以下形式:templatechar(&checkValid(...))[2];templatecharcheckValid(e);whereeisanexpression(usingtypeT)resultsintypeX如果e格式正确则结果将是(假设使用sizeof)1else2并且可以应用于:static_assert(sizeof(checkValid(0))==1,"");前几天我以不同的方式做了类似的事情:usingnamespacestd;template

c++ - 使用 c++ std::unique_ptr<> 或 std::shared_ptr<> 管理 objective-C 对象

Objective-C可以在某种程度上与C++混合使用canbecalledtoeachother.但是Objective-C对象或多或少仍然是手动管理的,并且语言中完全没有RAII惯用语。我想知道是否可以使用C++智能指针来管理Objective-C对象的生命周期。特别是现在boostscoped_ptr和shared_ptr都已添加到C++11标准中 最佳答案 ButObjective-Cobjectsstillaremoreorlessmanuallymanaged,andRAIIidiomisentirelyabsentfr

c++ - eclipse 火星 : Symbol 'unique_ptr' could not be resolved

使用EclipseMars,我遇到了Symbol'unique_ptr'couldnotberesolved错误。我尝试将-std=c++11添加到CDTGCC内置编译器设置,但这没有帮助。当我重新打开Eclipse时错误消失了,但是如果我修改代码,错误又回来了。一个简单的代码示例:std::unique_ptrp1; 最佳答案 在EclipseMars中打开Window>Preferences>C/C++>Build>Settings>Discovery>CDTGCCBuild-inCompilerSettings将-std=c+

c++ - 为什么 "error: invalid application of ' sizeof' 为使用 unique_ptr 的不完整类型”通过添加空析构函数来修复?

这个问题在这里已经有了答案:Isstd::unique_ptrrequiredtoknowthefulldefinitionofT?(9个回答)关闭7年前。我在类里面拉皮条STFT.在header中用这个编译就好了:classSTFT;//pimplofftopreventpointnameclashclassWhatever{private:STFT*stft;这在实现中:#include"STFT.h"Whatever::Whatever():stft(newSTFT()){//blahblah}Whatever::~Whatever(){deletestft;//pureevil

c++ - 我应该使用 QScopedPointer 还是 std::unique_ptr?

我正在开始一个新项目,使用Qt5和QMAKE_CXXFLAGS+=-std=c++1y。我不确定我应该更喜欢QScopedPointer还是std::unique_ptr。我读了somewhereQScopedPointer不再那么酷了。QScopedPointer是否有unique_ptr缺少的功能?在替换QScopedPointer时,是否有任何我不想使用的unique_ptr功能?还是相反? 最佳答案 QScopedPointer严格弱于unique_ptr因为它不支持移动语义。它的功能在其他方面非常相似。移动语义非常有用,不

c++ - 带有大括号初始化的 make_unique

https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique写道std::make_unique可以实现为templatestd::unique_ptrmake_unique(Args&&...args){returnstd::unique_ptr(newT(std::forward(args)...));}这不适用于没有构造函数的普通结构。这些可以用大括号初始化,但没有非默认构造函数。示例:#includestructpoint{intx,z;};intmain(){std::make_unique(1,2);}Com

c++ - "error: use of deleted function"在 move 构造函数中对 unique_ptr 调用 std::move 时

#includeclassA{public:A(){}A(constA&&rhs){a=std::move(rhs.a);}private:std::unique_ptra;};此代码无法使用g++4.8.4编译并抛出以下错误:error:useofdeletedfunction‘std::unique_ptr&std::unique_ptr::operator=(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delete]’a=std::move(rhs.a);^我知道unique_ptr的复制构造函数和复制赋值构造函数已删除