草庐IT

c++ - 递归函数中的返回类型推导

以下代码compiles:autofoo(inti){if(i==1)returni;elsereturnfoo(i-1)+i;}在关注doesn't时,c++1yautofoo(inti){return(i==1)?i:foo(i-1)+i;}为什么编译器不能在第二种情况下推导出返回类型?我在这里错过了什么吗?我知道在第二种情况下(i==1)之后有一个序列点,但这应该不会影响编译,对吧? 最佳答案 第一个工作是因为这个规则,7.1.6.4/11ofthelatestdraftOnceareturnstatementhasbeens

c++ - `auto` 引用的说明符类型推导

让我们考虑以下代码片段voidTest(){intx=0;int&rx=x;int*px=&x;autoapx=px;//deducedtypeisint*autoarx=rx;//deducedtypeisint}可以类比指针类型,期望arx的推导类型是int&,但实际上是int。标准中的规则是什么?其背后的原因是什么?有时我会在这样的情况下被它捕获:constBigClass&GetBigClass();...autoref_bigclass=GetBigClass();//unexpectedcopyisperformed 最佳答案

c++ - C++11 <type_traits> 模板参数类型推导失败

我正在尝试了解如何使用C++(11).这是我的简单测试程序#includetemplateinlineUadd(typenamestd::enable_if::value,U>::typea,typenamestd::enable_if::value,S>::typeb){returna+b;}intmain(intargc,constchar*argv[],constchar*envp[]){unsignedintui;inti;autoa=add(ui,i);return0;}当使用GCC4.8.1编译时,它会出错/home/per/f.cpp:Infunction‘intmain

c++ - variadic template template中的Variadic template推导

我不确定这个标题是否有意义,但这个例子实际上很简单://Aconverterstructwithagenericconstructor.templateclassTT>structconverter{templateconverter(constTT&);};//Afewclasstemplates.templatestructfoo{};templatestructfoo2{};templatestructfoo_variadic{};templatestructfoo_variadic2{};intmain(){//Allthesecompile.converter(foo{});

c++ - 在模板模板参数的情况下,由 decltype(auto) 推导的引用非类型模板参数是否可转发

还有一个decltype(auto)模板模板参数问题。这次我能够创建的最小代码来重现错误,如下所示:templateclassTT,decltype(auto)V>voidfoo(TT){};templatestructBar{};intx;intmain(){foo(Bar{});}这在[clang]结果:prog.cc:11:5:error:nomatchingfunctionforcallto'foo'foo(Bar{});^~~prog.cc:2:6:note:candidatetemplateignored:substitutionfailure[withTT=Bar]:no

c++ - 从运算符 T &() 中推导出 const

问题是不同的编译器产生不同的输出(clang/gcc),所以我认为这种用法是未定义的行为。然而,我的目标是在分配引用时推断const。输出:clang-3.6->不是constgcc-4.8.4->常量#include#includestructAnyReference{templateAnyReference(RT&a_var):_ptr(&a_var){}templateoperatorT&()const{if(std::is_const::value){std::cout(_ptr);}void*_ptr;};intmain(){inti(5);AnyReferencea(i);

c++ - decltype(auto) 从 lambda 捕获中推导出返回类型

我有编译器不同意一个小的C++14代码片段:#includestructunmovable{unmovable(){}unmovable(unmovable&&)=delete;};intmain(){unmovableu;autoi=[&]()->decltype(auto){returnu;};auto&uu=i();assert(&uu==&u);}该程序被g++4.9.3、g++-5.1.0、g++-5.2.0和VisualStudio2015接受,但不被clang++-3.7接受。clang++-3.7推断返回类型为unmovable(值)而不是unmovable&。如果程序

c++ - 在前向声明中自动推导返回类型并与旧函数语法混合

简介在C++11中,可以声明一个像这样的函数autotimes2(doublenum)->double;//A并像这样定义它doubletimes2(doublenum){//Breturnnum*2;}这对A,B也可以反过来混合。C++14引入了第三种方式autotimes2(doublenum){//Creturnnum;}问jar装风格C与A/B混合在声明/定义对中?可以C作为签名独立(当尚未提供函数体时)?//waitingforthedefinitiontoprovideinfoonreturntypeautotimes2(double); 最佳答

c++ - 理解模板参数推导

考虑以下代码:#includetemplateusingv_itt=typenamestd::vector::iterator;templatevoidfoo(v_itt){}intmain(){typenamestd::vector::iteratori=std::vector().begin();foo(i);//candidatetemplateignored:couldn'tinfertemplateargument'T'}DEMO代码有什么问题?我认为T应该被推断为long。有什么办法可以解决这个问题吗? 最佳答案 typ

c++ - 模板参数推导/替换失败,lambda 作为函数指针

我想知道为什么在下面的代码中编译器无法使用lambda作为函数foo()的参数(模板参数推导/替换失败),而一个简单的函数可以工作:templatevoidfoo(int(*)(Args...)){}intbar(int){return0;}intmain(){//foo([](int){return0;});//errorfoo(bar);return0;}intel编译器(版本18.0.3)template.cxx(12):error:noinstanceoffunctiontemplate"foo"matchestheargumentlistargumenttypesare:(l