草庐IT

c++ - 是否应允许 std::unique_ptr<void>

这是一个非常简单的问题。考虑以下代码:#include#includetypedefstd::unique_ptrUniqueVoidPtr;intmain(){UniqueVoidPtrp(newint);return0;}使用以下命令g++-std=c++0x-oprogfile.cpp用cygwin(g++4.5.3)编译就可以了。但是,使用microsoft编译器(VS2010或2013)编译时出现此错误:C:\ProgramFiles(x86)\MicrosoftVisualStudio10.0\VC\INCLUDE\memory(2067):errorC2070:'void

c++ - unique_ptr 线程安全吗?

unique_ptr线程安全吗?下面的代码不可能两次打印相同的数字吗?#include#include#include#includeusingnamespacestd;intmain(){unique_ptrwork;threadt1([&]{while(true){constunique_ptrlocalWork=move(work);if(localWork)printf("thread1:%d\n",*localWork);this_thread::yield();}});threadt2([&]{while(true){constunique_ptrlocalWork=mov

c++ - 原始指针和weak_ptr有什么区别?

如标题。这个问题可能已经有了答案,但我没有找到答案。 最佳答案 裸指针和weak_ptr之间的基本概念区别在于,如果指向的对象被销毁,裸指针不会告诉你。这称为悬空指针:指向不存在的对象的指针。它们通常很难追踪。weak_ptr会。为了使用weak_ptr,您必须首先将其转换为shared_ptr。如果那个shared_ptr没有指向任何东西,那么这个对象就被删除了。例如:#include#includestd::weak_ptrwp;voidtest(){autospt=wp.lock();//Hastobecopiedintoas

c++ - GCC 错误 : cannot convert 'const shared_ptr<...>' to 'bool' in return

我正在切换到GCC4.6.1,它开始提示在GCC4.4和MSVC10上运行良好的代码。从这样的函数返回时,它似乎不想在shared_ptr和bool之间进行转换:classClass{shared_ptrpointer_;};boolClass::Function()const{returnpointer_;}使用returnstatic_cast(pointer_);一切正常。到底他妈发生了什么?这是--std=cpp0x. 最佳答案 在C++11中,shared_ptr有一个explicitoperatorbool这意味着sha

c++ - 为什么 std::make_unique 而不是 std::unique_ptr::make?

为什么C++采用自由函数:std::make_unique(...);std::make_shared(...);而不是使用静态成员函数:std::unique_ptr::make(...);//staticstd::shared_ptr::make(...);//static? 最佳答案 TL;DR:静态成员函数始终可以访问私有(private)数据,但自由函数只有在明确标记为friend时才能访问私有(private)数据。选择将这些函数实现为自由函数(有一小部分实现为友元函数)不是随机的历史产物,而是一个经过深思熟虑的决定,以

C++ - 返回 const unique_ptr

我想知道为什么编译时会出错:conststd::unique_ptrget(){returnstd::make_unique(10);}intmain(){conststd::unique_ptrvalue=get();returnEXIT_SUCCESS;}我收到以下错误:main.cpp:Infunction‘intmain()’:main.cpp:10:44:error:useofdeletedfunction‘std::unique_ptr::unique_ptr(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delet

c++ - 带有 boost shared_ptr 的自定义(池)分配器

我想从一个池中分配由shared_ptr管理的对象,比如Boost的Pool接口(interface),如何实现? 最佳答案 这是执行您想要的操作的代码(可能无法编译,因为我手头没有boost并且我正在从内存中编写它):classYourClass;//yourdatatype,definedsomewhereelseboost::object_poolallocator;voiddestroy(YourClass*pointer){allocator.destroy(pointer);}boost::shared_ptrcreat

c++ - 为什么 libc++ 的 shared_ptr 实现使用完整的内存屏障而不是放松的?

在shared_ptr的boost实现中,它使用了relaxedmemoryorderingtoincrementitsreferencecount.这看起来很安全,因为减量使用获取/释放来确保在释放内存之前线程可以看到任何先前的减量。这个方法似乎是正确的,出现在HerbSutterstalkonatomics在libc++的实现中使用fullmemorybarrierstemplateinlineTincrement(T&t)_NOEXCEPT{return__sync_add_and_fetch(&t,1);}templateinlineTdecrement(T&t)_NOEXCE

mongodb - GeoJSON 和 MongoDB : Is it worth it to store points as GeoJSON. 点?

随着2.3>的引入,MongoDB在位置数据处理和查询方面变得更加有用。MongoDB将文档存储为BSON,因此每个文档都具有所有文档字段,这显然可能导致比我们传统的RMDBS更大的数据库。我曾经将折线和多边形存储为一系列索引点,并带有一个额外的字段表示每条线的顺序(我这样做是为了确保在使用JavaScript时的一致性,因此点并不总是以正确的方式存储命令)。是这样的:polyline:{[point:[0,0],order:0],[point:[0,1],order:1]}而现在我使用:polyline:{type:'LineString',coordinates:[[0,0],[1

mongodb - GeoJSON 和 MongoDB : Is it worth it to store points as GeoJSON. 点?

随着2.3>的引入,MongoDB在位置数据处理和查询方面变得更加有用。MongoDB将文档存储为BSON,因此每个文档都具有所有文档字段,这显然可能导致比我们传统的RMDBS更大的数据库。我曾经将折线和多边形存储为一系列索引点,并带有一个额外的字段表示每条线的顺序(我这样做是为了确保在使用JavaScript时的一致性,因此点并不总是以正确的方式存储命令)。是这样的:polyline:{[point:[0,0],order:0],[point:[0,1],order:1]}而现在我使用:polyline:{type:'LineString',coordinates:[[0,0],[1