草庐IT

c++ - 为什么自动返回类型在此示例中会丢失移动语义?

我在看videoNicolai说auto在这个例子中失去了移动语义:templateautocall(Callable&&op,Args&&...args){returnstd::invoke(std::forward(op),std::forward(args)...);}我在想:为什么会这样?做guaranteedRVO启动这个例子?如果是这样,担心搬家有什么意义? 最佳答案 我认为Nicolai可以将其表述得更好一些。当您通过auto返回时,您的函数返回一个值(将推导其类型)。如果std::invoke返回一个纯右值或一个xv

c++ - 结构化绑定(bind)是否可重用?

我正在使用Windows10、VisualStudio2017v15.7.1和/std:c++latest/permissive-这段带有结构化绑定(bind)的简单代码无法编译:auto[a,b]=func1(x,y,z);//autofunc1()->std::tuple[a,b]=func2(x,y,z);//samesignatureasfunc2说此处不允许使用E1277属性。下面的代码也不会编译,同样的错误doublea,b;[a,b]=func1(x,y,z);[a,b]=func2(x,y,z);代码auto[a,b]=func1(x,y,z);auto[a,b]=fu

c++ - 私有(private)方法作为尾随返回类型 (decltype)

当我尝试在私有(private)方法函数上使用decltype()时,我得到了私有(private)方法error:'m1'hasnotbeendeclaredinthisscope#includeclassC{public:C()=default;~C()=default;automasterMethod(intopMode)->decltype(m1()){switch(opMode){case1:returnm1();break;case2:returnm2();break;default:returnm1();break;}}private:intm1(){return1;}i

c++ - 尾随返回类型中占位符类型的用途是什么?

根据[dcl.fct]/2下面的片段是合法的。GCCandclangcompileandexecutethecode,#includeinti=-1;autof()->auto&&{returni;}intmain(){f()=2;std::cout打印2但是在C++中允许这样做的目的是什么?在上面的示例中,只需将trailing-return-type替换为int&即可获得相同的结果。换句话说,我正在寻找一个示例,其中包含占位符类型的trailing-return-type是有意义的。 最佳答案 您可以就一致性提出争论:您可以将其

c++ - 为什么在 C++11/C++14 中有针对自动和大括号初始化器的特殊类型推导规则?

在他的CppCon2014演讲中"TypeDeductionandWhyYouCare",ScottMeyers提出了一个问题,为什么在C++11/C++14标准中有关于auto和大括号初始化器的特殊规则(他的问题从at36m05s开始)。auto与braced-init-list组合的语义在§7.1.6.4/6中定义。我想了想,也想不出一个用例。到目前为止,我所看到的最接近的例子是BjarneStroustrup使用它的一个例子。在他的Cpp2014talk"MakeSimpleTasksSimple!",他曾经使用auto来捕获初始化器(但只是作为一种变通方法)。这是代码(幻灯片3

c++ - 遍历动态 vector 时 auto 的异常行为

我正在使用自动遍历vector(附加代码)。在遍历的同时,我还在后面附加了一些元素。我没想到会得到这样的输出。#include#includeusingnamespacestd;vectordynamic_vector;voidaccess(){for(autoi:dynamic_vector){if(i==3){dynamic_vector.push_back(4);dynamic_vector.push_back(5);}cout输出:123我原以为从1到5的所有数字都会被打印出来。我无法理解如何使用auto进行遍历? 最佳答案

c++ - 动态转换引用和自动

我在使用auto和dynamic_cast时遇到了一个非常奇怪的行为。这是我的类层次结构:classBaseInterface{public:virtualvoidsomeMethod()=0;};classDerived:publicBaseInterface{public:virtualvoidsomeMethod1()=0;voidsomeMethod()override;};当然还有一些实现所有派生方法的类。然后是第三个类,如下所示:classThirdClass{public:voiddemoMethod(BaseInterface&);voidanotherMethod(D

c++ - lambdas 中的模板参数列表中的 auto 是标准的一部分吗?

今天,我偶然发现了以下代码片段:#includeintmain(){autoa=[](std::pairvalue){};a(std::pair{3,true});}http://cpp.sh/5p34我只有一个问题:标准支持这段代码吗?它在GCC中编译(使用-std=c++14),但不是clang或VisualStudio2015(VC++14)。这似乎应该成为标准的一部分,因为如果lambda应该具有与常规函数相同的模板支持,那么应该支持它。这似乎可以转换为所有模板类型,而不仅仅是std::pair。 最佳答案 在C++14中,

c++ - const auto 和 auto const 如何应用于指针?

我尝试了一些代码,想知道在使用auto时C++中的const限定符如何应用于指针类型。intmain(){intfoo=1;intbar=2;//Expected:constint*ptr_to_const_int=&foo;constautoptr_to_const_int=&foo;//Expected:int*constconst_ptr_to_int=&foo;autoconstconst_ptr_to_int=&foo;*ptr_to_const_int=3;//Thoughtthiswoulderror//ptr_to_const_int=&bar;Thisdoeserro

c++ - 如何初始化用 auto 关键字声明的循环计数器?

这是我的代码:#include#includevoidcumulative_sum_with_decay(std::vector&v){for(autoi=2;i&v){std::cout{1,2,3,4,5,6,7,8,9,10};cumulative_sum_with_decay(v);printv(v);}当我尝试编译和运行这个程序时,我收到了这些警告:$clang++-std=c++11-Wextrafoo.cpp&&./a.outfoo.cpp:6:24:warning:comparisonofintegersofdifferentsigns:'int'and'std::__