草庐IT

make_scoped

全部标签

c++ - 使用自定义删除器 boost scoped_ptr/scoped_array

我不知道如何让scoped_ptr或scoped_array使用自定义删除器。也许还有另一种实现类似于shared_ptr允许受控删除?顺便说一句,为什么shared_ptr允许自定义删除器而scoped_ptr不允许?只是好奇。 最佳答案 Idon'tseehowtogetscoped_ptrorscoped_arraytousecustomdeleter你不能。Maybethereisanotherimplementationwhichallowscontrolleddeletionsimilartoshared_ptr?如果您

c++ - 使用 C++11 时扩展命名空间 std 以实现 make_unique

我遇到了一个固定在C++11功能上但实现了std::make_unique的代码库。如果不使用C++14,则扩展namespacestd以添加功能,即围绕实现包装#ifdefined(__cplusplus)&&__cplusplus我知道那是undefinedbehaviortoextendnamespacestd(有一些异常(exception))。上述情况仍然可以接受还是应该避免? 最佳答案 不,这是被禁止的——尽管,通过#definemake_unique???符合标准的C++11程序可以非常确定库从不提及名称(在字符串化之

C++:引用 "out of scope"对象

关于引用文献,有一件事我一直不明白,我希望有人能帮助我。据我所知,引用不能为空。但是如果你有一个函数foo()返回对堆栈对象的引用会发生什么:Object&foo(){Objecto;returno;}Object&ref=foo();理论上ref将引用一个不存在的对象,因为一旦函数返回,o就会超出范围。这里发生了什么? 最佳答案 这会导致未定义的行为。不要这样做。在实现方面,实际上,引用将指向调用foo的堆栈框架所在的堆栈。在许多情况下,该内存仍然有意义,因此错误通常不会立即显现出来。因此,您应该注意永远不要创建这样的悬空引用。

C++11 传递 'this' 作为 std::make_shared 的参数

我正在尝试使用std::make_shared将“this”传递给构造函数例子://headersclassA{public:std::shared_ptrcreateB();}classB{private:std::shared_ptra;public:B(std::shared_ptr);}//sourcestd::shared_ptrA::createB(){autob=std::make_shared(this);//Compilererror(VS11Beta)autob=std::make_shared(std::shared_ptr(this));//Nocompiler

c++ - 使用 std::make_shared 抽象类实例化时出错

我将省略相当多的代码,因为这些是一些相当大的对象,而我的问题实际上只涉及std::make_shared的操作。我在名为D3D11Shader的命名空间SYNC中有一个对象。这有一个名为static的函数,SYNC::D3D11Shader*SYNC::D3D11Shader::CreateInstance(conststd::string&s)它将采用字符串索引并返回指向从SYNC::D3D11Shader派生的着色器实例的指针。有一次我开始使用智能指针在包含所有这些着色器的vector中自动重新分配它们。然而,当我去做这件事时,std::shared_ptrshaderPtr;//

c++ - make 失败,返回错误 "cannot convert ‘std::istream {aka std::basic_istream<char>}’ 到 ‘bool’”

我正在尝试从源代码编译libgtextutils(fastxtoolkit需要)。“./configure”命令运行良好,但随后的“make”命令产生了一个我无法解决的错误。text_line_reader.cpp:Inmemberfunction‘boolTextLineReader::next_line()’:text_line_reader.cpp:47:9:error:cannotconvert‘std::istream{akastd::basic_istream}’to‘bool’inreturnreturninput_stream;^~~~~~~~~~~~make[3]:*

c++ - "Nested"scoped_lock

我缩短的简化类如下所示:classA{public://...methodA();methodB();protected:mutableboost::mutexm_mutex;sometype*m_myVar;}A::methodA(intsomeParam){boost::mutex::scoped_lockmyLock(m_mutex);m_myVar->doSomethingElse();}A::methodB(intsomeParam){boost::mutex::scoped_lockmyLock(m_mutex);m_myVar->doSomething();this->m

c++ - g++ 和 clang++ 在 `std::make_index_sequence` 和 `std::index_sequence` 用于模板参数默认类型时的不同行为

另一个“g++和clang++之间谁是正确的?”C++标准专家的问题。给定以下代码#includetemplate>structfoo;templatestructfoo>{};templatevoidbar(fooconst&){}intmain(){bar(foo{});}我看到g++编译时clang++给出了以下错误tmp_003-14,gcc,clang.cpp:32:4:error:nomatchingfunctionforcallto'bar'bar(foo{});^~~tmp_003-14,gcc,clang.cpp:27:6:note:candidatetemplate

c++ - 如何使用 std::scoped_allocator_adapter?

据我了解,std::scoped_allocator_adapter提供一种控制机制,用于指定单独哪个分配器将由容器、其元素、其元素的元素等使用,假设元素本身是容器。也就是说,我无法理解std::scoped_allocator_adapter的语义.BjarneStroustrup在TheC++ProgrammingLanguage,section34.4.4,pg中提供了以下4个示例。1001(在接下来的问题中,我将它们称为Example-1、Example-2等。):Wehavefouralternativesforallocationofvectorsofstrings://v

c++ - GNU Make 产生完全不同的结果

这令人困惑。我有我的Makefile:OBJECTS=INCLUDE_BUILD_PATH=/Users/wen/Projects/include#ChangecompilationsettingshereCOMPILE=g++overrideCOMPILE_FLAGS+=-O2#Changelinker/compilerspecificsettingshereLD_FLAGS:=CC_FLAGS:=-c-I$(INCLUDE_BUILD_PATH)/bigint#AddsourceextensionshereSRC_EXT=cppcc#Addheaderdependenciesher