草庐IT

auto_ptr_ref

全部标签

c++ - 带 auto 的 initializer_list 包含多个表达式

相当简单的问题,autox11{1,2,3,4};autox1={1,2,3,4};autox22{1.0,2.25,3.5};autox2={1.0,2.25,3.5};据我了解,这里是否有=应该没有区别。但是,使用llvm/clang6.0.0(使用--std=c++17),我得到:main1.cpp:35:17:error:initializerforvariable'x11'withtype'auto'containsmultipleexpressionsautox11{1,2,3,4};~~~~~~~~^main1.cpp:37:20:error:initializerfor

c++ - auto 作为常规函数中的参数是 GCC 4.9 扩展吗?

gcc4.9允许以下代码,但gcc4.8和clang3.5.0拒绝它。voidfoo(autoc){std::cout我得到warning:ISOC++forbidsuseof'auto'inparameterdeclaration[-Wpedantic]在4.9但在4.8和clang我得到error:parameterdeclared'auto'. 最佳答案 是的,这是一个扩展。我相信它可能会作为“概念”提案的一部分添加到C++17中。 关于c++-auto作为常规函数中的参数是GCC

c++ - auto 作为常规函数中的参数是 GCC 4.9 扩展吗?

gcc4.9允许以下代码,但gcc4.8和clang3.5.0拒绝它。voidfoo(autoc){std::cout我得到warning:ISOC++forbidsuseof'auto'inparameterdeclaration[-Wpedantic]在4.9但在4.8和clang我得到error:parameterdeclared'auto'. 最佳答案 是的,这是一个扩展。我相信它可能会作为“概念”提案的一部分添加到C++17中。 关于c++-auto作为常规函数中的参数是GCC

c++ - 使用 std::vector< std::shared_ptr<const T>> 是反模式吗?

很长一段时间我都在使用std::vector和std::shared_ptr手牵手。最近开始使用std::shared_ptr每当需要指向const对象的指针时。没关系,因为std::shared_ptr可以转换为std::shared_ptr然后他们共享相同的引用计数器,一切都感觉很自然。但是当我尝试使用std::vector>等结构时我遇到了麻烦。为简化起见,我将表示这两种结构:templateusingSharedPtrVector=std::vector>;templateusingSharedConstPtrVector=std::vector>;问题是虽然SharedPtr

c++ - 使用 std::vector< std::shared_ptr<const T>> 是反模式吗?

很长一段时间我都在使用std::vector和std::shared_ptr手牵手。最近开始使用std::shared_ptr每当需要指向const对象的指针时。没关系,因为std::shared_ptr可以转换为std::shared_ptr然后他们共享相同的引用计数器,一切都感觉很自然。但是当我尝试使用std::vector>等结构时我遇到了麻烦。为简化起见,我将表示这两种结构:templateusingSharedPtrVector=std::vector>;templateusingSharedConstPtrVector=std::vector>;问题是虽然SharedPtr

c++ - 什么时候需要使用 std::ref ?

考虑:std::tuplefunc(constA&a){returnstd::make_tuple(0,std::ref(a));}std::ref是编写正确且可移植的代码所必需的吗?(没有它也可以编译)背景:如果我删除std::ref我的代码构建良好,没有任何警告(g++-4.6-Wall),但不能正确运行。如果感兴趣,A的定义:structA{std::arrayvec;typedefinttype_t;templateA&operator=(conststd::pair,std::tuple>&e){for(inti=0;i 最佳答案

c++ - 什么时候需要使用 std::ref ?

考虑:std::tuplefunc(constA&a){returnstd::make_tuple(0,std::ref(a));}std::ref是编写正确且可移植的代码所必需的吗?(没有它也可以编译)背景:如果我删除std::ref我的代码构建良好,没有任何警告(g++-4.6-Wall),但不能正确运行。如果感兴趣,A的定义:structA{std::arrayvec;typedefinttype_t;templateA&operator=(conststd::pair,std::tuple>&e){for(inti=0;i 最佳答案

c++ - 带 malloc 和 free 的 shared_ptr

我在包含c和cpp的大型应用程序中工作。所有文件都保存为cpp扩展名,但代码是用c-style编写的。我的意思是它是定义结构而不是通过malloc和realloc和calloc分配内存的类。最近他们已经安装了boost库所以我打算在我现有的代码库中使用所以我有一些以下问题。我可以将std::shared_ptr与malloc和free一起使用吗?如果是,谁能指出我的示例代码库?如果我在我的应用程序中创建std::shared_ptr并将此指针传递给另一个使用malloc或calloc的函数,它会影响任何功能吗?或者换句话说:对于以下代码,如何使用std::shared_ptr实现类似的

c++ - 带 malloc 和 free 的 shared_ptr

我在包含c和cpp的大型应用程序中工作。所有文件都保存为cpp扩展名,但代码是用c-style编写的。我的意思是它是定义结构而不是通过malloc和realloc和calloc分配内存的类。最近他们已经安装了boost库所以我打算在我现有的代码库中使用所以我有一些以下问题。我可以将std::shared_ptr与malloc和free一起使用吗?如果是,谁能指出我的示例代码库?如果我在我的应用程序中创建std::shared_ptr并将此指针传递给另一个使用malloc或calloc的函数,它会影响任何功能吗?或者换句话说:对于以下代码,如何使用std::shared_ptr实现类似的

c++ - 在 std::move() 之后 unique_ptr 会发生什么?

这段代码就是我想做的:Tony&Movie::addTony(){Tony*newTony=newTony;std::unique_ptrtony(newTony);attachActor(std::move(tony));return*newTony;}我想知道我是否可以这样做:Tony&Movie::addTony(){std::unique_ptrtony(newTony);attachActor(std::move(tony));return*tony.get();}但是*tony.get()会是同一个指针还是null?我知道我可以验证,但它的标准做法是什么?