草庐IT

auto_ptr_ref

全部标签

c++ - 使用 C++ 尾随返回类型时 auto 是什么意思?

不同于平常voidfoo(void){coutC++11允许是另一种选择,使用尾随返回autobar(void)->void{cout在后者中-auto旨在表示什么?另一个例子,考虑函数autofunc(inti)->int(*)[10]{}同样的问题,这个例子中auto是什么意思? 最佳答案 一般来说,C++11中的新关键字auto表示应该从表达式的结果中推断出表达式的类型(在这种情况下是函数的返回类型),在这种情况下,->之后会发生什么。没有它,函数将没有类型(因此不是函数),编译器最终会感到困惑。

c++ - 将 unique_ptr 的 vector 插入另一个 vector

我有一个unique_ptr的vector,我想将它们附加到另一个unique_ptrvector。我通常会做一个简单的插入:std::vector>bar;bar.push_back(std::unique_ptr(newfoo(1)));std::vector>baz;baz.push_back(std::unique_ptr(newfoo(2)));bar.insert(bar.end(),baz.begin(),baz.end());但是这给了我类似这样的编译错误:/usr/include/c++/4.8/bits/stl_algobase.h:335:error:useofd

c++ - 将 unique_ptr 的 vector 插入另一个 vector

我有一个unique_ptr的vector,我想将它们附加到另一个unique_ptrvector。我通常会做一个简单的插入:std::vector>bar;bar.push_back(std::unique_ptr(newfoo(1)));std::vector>baz;baz.push_back(std::unique_ptr(newfoo(2)));bar.insert(bar.end(),baz.begin(),baz.end());但是这给了我类似这样的编译错误:/usr/include/c++/4.8/bits/stl_algobase.h:335:error:useofd

c++ - 如何使用带有 boost::ptr_map 的 BOOST_FOREACH?

如何通过boost::ptr_map有效地使用BOOST_FOREACH(字符数/可读性)?Kristo在他的answer中展示了可以将BOOST_FOREACH与ptr_map一起使用,但与使用迭代器迭代ptr_map相比,它并不能真正为我节省任何打字(或使我的代码更易读):typedefboost::ptr_container_detail::ref_pairIntPair;BOOST_FOREACH(IntPairp,mymap){inti=p.first;}//vs.boost::ptr_map::iteratorit;for(it=mymap.begin();it!=myma

c++ - 如何使用带有 boost::ptr_map 的 BOOST_FOREACH?

如何通过boost::ptr_map有效地使用BOOST_FOREACH(字符数/可读性)?Kristo在他的answer中展示了可以将BOOST_FOREACH与ptr_map一起使用,但与使用迭代器迭代ptr_map相比,它并不能真正为我节省任何打字(或使我的代码更易读):typedefboost::ptr_container_detail::ref_pairIntPair;BOOST_FOREACH(IntPairp,mymap){inti=p.first;}//vs.boost::ptr_map::iteratorit;for(it=mymap.begin();it!=myma

c++ - 为什么 "auto const&"不是只读的?

这个问题在这里已经有了答案:Variablesmarkedasconstusingstructuredbindingsarenotconst(1个回答)关闭4年前.#includeintmain(){intxa=1;intya=2;autoconst&[xb,yb]=std::tuple(xa,ya);xb=9;//Shouldn'tthisberead-only?returnxa+ya;}这不仅编译,而且返回11。那么两个问题:为什么当xb被指定为autoconst&时我可以写入?这不应该编译失败吗?为什么我不能用“auto&”替换“autoconst&”并让它编译?Clang(6.

c++ - 为什么 "auto const&"不是只读的?

这个问题在这里已经有了答案:Variablesmarkedasconstusingstructuredbindingsarenotconst(1个回答)关闭4年前.#includeintmain(){intxa=1;intya=2;autoconst&[xb,yb]=std::tuple(xa,ya);xb=9;//Shouldn'tthisberead-only?returnxa+ya;}这不仅编译,而且返回11。那么两个问题:为什么当xb被指定为autoconst&时我可以写入?这不应该编译失败吗?为什么我不能用“auto&”替换“autoconst&”并让它编译?Clang(6.

c++ - `unique_ptr< T const [] >` 是否应该接受 `T*` 构造函数参数?

代码:#includeusingnamespacestd;structT{};T*foo(){returnnewT;}Tconst*bar(){returnfoo();}intmain(){unique_ptrp1(bar());//OKunique_ptra1(bar());//OKunique_ptrp2(foo());//OKunique_ptra2(foo());//?thisisline#15}VisualC++10.0和MinGWg++4.4.1的示例错误:[d:\dev\test]>clfoo.cppfoo.cppfoo.cpp(15):errorC2248:'std::

c++ - `unique_ptr< T const [] >` 是否应该接受 `T*` 构造函数参数?

代码:#includeusingnamespacestd;structT{};T*foo(){returnnewT;}Tconst*bar(){returnfoo();}intmain(){unique_ptrp1(bar());//OKunique_ptra1(bar());//OKunique_ptrp2(foo());//OKunique_ptra2(foo());//?thisisline#15}VisualC++10.0和MinGWg++4.4.1的示例错误:[d:\dev\test]>clfoo.cppfoo.cppfoo.cpp(15):errorC2248:'std::

c++ - 在 vector <unique_ptr> 上使用 is_copy_constructible 误报

类型trait是否应该能够处理std::vector>之类的情况?并检测到它不是可复制的?这是https://ideone.com/gbcRUa的示例(运行g++4.8.1)#include#include#include#includeintmain(){//Thisprints1,implyingthatit'scopyconstructible,whenit'sclearlynotstd::cout>>::value如果这是is_copy_constructible的正确行为,有没有办法检测到复制结构是不正确的?好吧,不仅仅是让它无法编译。 最佳答案