草庐IT

auto_ptr

全部标签

c++ - auto 用于容器的类型是什么?

我可以通过在C++中使用不同的容器来实现相同的输出。例如。.std::arrayv={1,2,3,4,5};for(autoi:v)std::cout或std::vectorv={1,2,3,4,5};或intv[]={1,2,3,4,5};等等。.那么auto在这里使用什么容器呢?autov={1,2,3,4,5};for(autoi:v)std::cout 最佳答案 std::initializer_list自己检查并不难,您可以随时decltype(v),然后将其与所述列表类型进行比较。它还有另一个很好的属性,有时非常有用,你

c++ - auto 用于容器的类型是什么?

我可以通过在C++中使用不同的容器来实现相同的输出。例如。.std::arrayv={1,2,3,4,5};for(autoi:v)std::cout或std::vectorv={1,2,3,4,5};或intv[]={1,2,3,4,5};等等。.那么auto在这里使用什么容器呢?autov={1,2,3,4,5};for(autoi:v)std::cout 最佳答案 std::initializer_list自己检查并不难,您可以随时decltype(v),然后将其与所述列表类型进行比较。它还有另一个很好的属性,有时非常有用,你

c++ - 为什么在使用结构化绑定(bind)时 auto 似乎会推断出引用?

考虑以下使用来自C++17的结构化绑定(bind)的代码:inta=0,b=0;auto[x,y]=std::tie(a,b);y=1;std::cout由于我使用了auto,我希望代码打印00因为y应该是一个拷贝。但是,它会打印01。为什么?我认为一个简单的auto永远不会推断出引用。 最佳答案 作为cppreference注意,[之前的声明部分(即auto在您的情况下)不适用于引入的标识符。相反,它适用于由编译器在幕后创建的隐藏变量。您的结构化绑定(bind)声明auto[x,y]=std::tie(a,b);大致相当于auto

c++ - 为什么在使用结构化绑定(bind)时 auto 似乎会推断出引用?

考虑以下使用来自C++17的结构化绑定(bind)的代码:inta=0,b=0;auto[x,y]=std::tie(a,b);y=1;std::cout由于我使用了auto,我希望代码打印00因为y应该是一个拷贝。但是,它会打印01。为什么?我认为一个简单的auto永远不会推断出引用。 最佳答案 作为cppreference注意,[之前的声明部分(即auto在您的情况下)不适用于引入的标识符。相反,它适用于由编译器在幕后创建的隐藏变量。您的结构化绑定(bind)声明auto[x,y]=std::tie(a,b);大致相当于auto

c++ - std::make_shared、std::unique_ptr 和移动构造函数

以下代码使用clang3.0/libc++编译:#includeclassFoo{public:Foo():mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared();return0;}但是这个没有(std::string参数加了):#include#includeclassFoo{public:Foo(conststd::string&s):mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared("

c++ - std::make_shared、std::unique_ptr 和移动构造函数

以下代码使用clang3.0/libc++编译:#includeclassFoo{public:Foo():mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared();return0;}但是这个没有(std::string参数加了):#include#includeclassFoo{public:Foo(conststd::string&s):mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared("

c++ - 分配给 std::shared_ptr 成员变量

我有一个类foo,与成员bar类型std::shared_ptr:classfoo{std::shared_ptrbar;/*otherstuffhere*/};在那个类(class)我想分配一个newint至bar.但是我不会写bar=newint();因为指针没有公共(public)赋值运算符。应该如何我这样做?我可以std::move或std::swap但这些似乎都不对。 最佳答案 bar=std::make_shared();是一种方法,特别是如果您希望保留赋值运算符的易处理性。

c++ - 分配给 std::shared_ptr 成员变量

我有一个类foo,与成员bar类型std::shared_ptr:classfoo{std::shared_ptrbar;/*otherstuffhere*/};在那个类(class)我想分配一个newint至bar.但是我不会写bar=newint();因为指针没有公共(public)赋值运算符。应该如何我这样做?我可以std::move或std::swap但这些似乎都不对。 最佳答案 bar=std::make_shared();是一种方法,特别是如果您希望保留赋值运算符的易处理性。

c++ - 将 std::shared_ptr 与 clang++ 和 libstdc++ 一起使用

我正在尝试使用libstdc++(4.6.1)在clang++(clang版本3.1(trunk143100))中使用std::shared_ptr。我有一个小演示程序:#includeintmain(){std::shared_ptrsome(newint);std::shared_ptrother(some);return0;}可以使用:clang++-std=c++0x-omainmain.cpp并给出以下错误输出:main.cpp:6:23:error:calltodeletedconstructorof'std::shared_ptr'std::shared_ptrother

c++ - 将 std::shared_ptr 与 clang++ 和 libstdc++ 一起使用

我正在尝试使用libstdc++(4.6.1)在clang++(clang版本3.1(trunk143100))中使用std::shared_ptr。我有一个小演示程序:#includeintmain(){std::shared_ptrsome(newint);std::shared_ptrother(some);return0;}可以使用:clang++-std=c++0x-omainmain.cpp并给出以下错误输出:main.cpp:6:23:error:calltodeletedconstructorof'std::shared_ptr'std::shared_ptrother