jjzjj

decltype

全部标签

c++ - 在 lambda 中完美转发?

有了函数,可以这样写:templatevoidf(T&&x){myfunction(std::forward(x));}但是对于lambda,我们没有T:autof=[](auto&&x){myfunction(std::forward(x));}如何在lambda中进行完美转发?decltype(x)是否用作std::forward中的类型? 最佳答案 转发绑定(bind)到转发引用的lambda参数的规范方法确实是使用decltype:autof=[](auto&&x){myfunction(std::forward(x));}

c++ - 在成员函数尾随返回类型中使用 this 和属性?

在此answer我给出了,在尾随返回类型中使用this和类_arg的属性作为decltype表达式的一部分是有意义的。可以不用,但不方便。既不是clang3.0(见下文)也不是gcc4.5.2不过还是接受了。#includeclassMyClass{public:MyClass(inti):_arg(i){}templateautoapply(F&f)->decltype(f(_arg)){returnf(_arg);}templateautoapply(F&f)->decltype(f(*this,_arg)){returnf(*this,_arg);}private:int_arg

c++ - 尾随返回类型中的占位符是否会覆盖初始占位符?

g++似乎接受auto和decltype(auto)的任意组合作为初始和尾随返回类型:inta;autof(){return(a);}//intautog()->auto{return(a);}//intautoh()->decltype(auto){return(a);}//int&decltype(auto)i(){return(a);}//int&decltype(auto)j()->auto{return(a);}//intdecltype(auto)k()->decltype(auto){return(a);}//int&但是,clang拒绝j和k,说:error:funct

c++ - 在 C++ 中 decltype 取消引用的指针

有人可以向我解释为什么我不能按照以下方式做某事吗:int*b=newint(5);int*c=newdecltype(*b)(5);cout这将引发C464“int&”:无法使用“new”分配引用。我将如何执行这样的操作?我需要的是我发送的变量的取消引用基类型。这虽然有效int*b=newint(5);int**a=newint*(b);decltype(*a)c=*a;cout我明白上面的代码是如何工作的,但我如何使用new执行类似的操作? 最佳答案 解引用运算符*返回一个不能使用new分配的引用.相反,您可以使用std::rem

c++ - 如何定义具有非尾随 decltype 返回类型的外联类模板成员函数

templatestructfoo{intx;decltype(x)f1();};似乎不可能定义f1out-of-line。我尝试了以下定义,但均无效:templatedecltype(x)foo::f1(){}templateautofoo::f1()->decltype(x){}templateautofoo::f1(){returnx;}templatedecltype(std::declval>().x)foo::f1(){}//Thisreturntypeiscopiedfromthegccerrormessagetemplatedecltype(((foo*)(void)0

c++ - 混合 decltype 和 enable_if

似乎将decltype与SFINAEenable_if一起使用并不简单。我尝试以三种不同的方式使用enable_if编写go。所有这些都因编译器错误而失败(GCC的字面意思是:“错误:'thing'不是'foo'的成员”和实例化上下文)。#includestructfoo{enum{has_thing=false};};structbar{enum{has_thing=true};staticintthing(){return0;}};templatestructTest{/*autogo(typenamestd::enable_if::type=0)->decltype(T::thi

c++ - 可变参数模板求和运算左关联

下面的代码适用于:左结合求和运算的目标:sum(1,2,3,4);但是,对于sum(1,2,3,4,5)或sum(1,2,3,4,5,...)。超过4个参数的任何内容都会出现错误:error:nomatchingfunctionforcalltosum(int,int,int,int,int)=================================templateTsum(constT&v){returnv;}templateautosum(constT1&v1,constT2&v2)->decltype(v1+v2){returnv1+v2;}templateautosum

c++ - 为什么在 lambda 中通过引用捕获不会改变变量的类型?

我认为通过引用捕获会改变变量的类型。让我们考虑以下示例:#include#includeintmain(){intx=0;int&x_ref=x;constintx_const=x;constint&x_const_ref=x_const;autolambda=[&](){static_assert(std::is_same::value,"!");static_assert(std::is_same::value,"!");static_assert(std::is_same::value,"!");static_assert(std::is_same::value,"!");};l

c++ - decltype(constexpr 变量)

为什么constexpr变量的decltype失败?#include#includeconstexpruint16_tfoo(){return0;}constexprautocv=foo();autov=foo();static_assert(std::is_same::value,"!");//failedstatic_assert(std::is_same::value,"!");//success 最佳答案 decltype(entity)指定此表达式指定的实体的声明类型。由于constexpr,(对象声明中使用的conste

C++ 箭头类型产生左值

根据C++Primer,C++箭头运算符产生一个左值。此外,产生左值的表达式的decltype将产生引用类型。那么为什么以下decltype不会导致引用类型。structMyStruct{stringname};MyStructs;s.name="aname";MyStruct*p=&s;decltype(p->name)str=s.name;//typeofstrwillbestringandnot&stringalthoughp->nameyieldsanlvalue 最佳答案 来自cppreferenceIftheargume