草庐IT

decltype

全部标签

c++ - 获取函数的 decltype

我想获取函数的类型并创建它的std::vector。例如,我有intfoo(inta[],intn){return1;}intbar(inta[],intn){return2;}像这样的函数vector将是:std::vector>v;一般来说,decltype()会更好,例如:std::vectorv;但是,这会导致编译错误。我猜是因为decltype()无法区分int(*func)(int[],int)std::function有办法解决这个问题吗? 最佳答案 使用其中之一:std::vectorv;或:std::vectorv

c++ - 与静态方法中的 decltype(*this) 等价?

我有一些宏需要访问当前类的类型,目前我通过违反DRY的模式解决了这个问题:structThisScruct{inta;doubleb;//examplestaticmethodusingthis-purelyexample-notfullusecasestaticsize_tsum_offsets(){typedefThisStructSelfT;returnoffsetof(SelfT,a)+offsetof(SelfT,b);}};这在使用offsetof时经常出现。关键字,至少在我自己的作品中是这样。现在,在您锁定this无法通过静态方法访问之前-意识到我只想知道如何以通用/宏友

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++ - 将 decltype 与成员函数指针一起使用

我在为成员函数指针使用decltype时遇到了一些问题:#include#includestructA{voidfunc1(){}typedefdecltype(&A::func1)type;};intwmain(intargc,wchar_t*argv[]){typedefdecltype(&A::func1)type;//Case1std::wcout::value::value案例1按预期打印true,但案例2打印false。decltype是否剥离了类型的“成员”属性?如果是,为什么?此外,有没有办法防止这种行为?无论我在哪里使用decltype,我都需要获取成员函数的类型。请

c++ - 你能在对象函数上用 decltype 声明一个成员变量吗?

structExample{boost::tokenizer>tokens;decltype(tokens.begin())i;};在VisualStudio2013上,我收到编译器错误C2228:“.begin”左侧必须有类/结构/union。这是有效的C++11代码吗?如果不是,是否有一种方法可以在不为迭代器键入长模板类型的情况下执行此操作?我认为decltype应该起作用的逻辑是编译器绝对可以看到函数签名,所以我认为您可以根据其返回类型声明一个变量。 最佳答案 您的代码有效。这是knownVSbug.链接的错误报告中的示例类似

c++ - decltype 可以声明一个右值吗?

//CompiledbyVisualStudio2012structA{booloperator==(constA&other)const{for(decltype(this->n)i=0;in)i=0;i这是VC++2012的错误吗? 最佳答案 这似乎是一个VS2012编译器错误。规范在7.1.6.2节的第4段中对此非常清楚。实际上,给出的示例之一显示了通过常量指针a引用的表达式。decltype(a->x)产生double,而decltype((a->x))产生doubleconst&.所以这是一个错误;编译器认为i是const

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++ - decltype(std) 是否合法,是否有任何用途?

当在命名空间周围使用decltype时,我可以编写编译代码,但该语句在g++4.9.1下似乎没有任何效果,在clang下它生成错误:意外的命名空间名称“std”:预期的表达式例如,以下所有代码都在g++下编译,但程序集不显示为它们生成的任何代码。usings=decltype(std);auton=typeid(decltype(std)).name();autosz=n.size();std::printf("sizeis%zu\n",sz+1);std::printf("thistypeis:%s\n\n",n.c_str());//theonlylimitisyourimagin

c++ - 函数返回的 decltype

我正在制作一个模板化类,它是任何迭代器的包装器。我正在以这种方式制作运算符(operator)*:templateclassMyIterator{public://...decltype(*T())operator*(){//...}}我给decltype一个对类T的operator*的调用,它甚至可以工作,但如果T没有默认构造函数,它就不会工作。有没有办法找出函数/方法的返回类型? 最佳答案 这就是std::declval的用途:decltype(*std::declval())operator*(){/*...*/}如果你的实现

c++ - 自动返回类型扣除警告 : why do we need decltype when return defines the type anyway?

这是一个关于elementsSize()成员函数做什么的问题,关于自动返回类型推导:#include#includetemplateclassElementVector{std::vectorelementVec_;//Otherattributes.public:ElementVector()=default;ElementVector(conststd::initializer_list&list):elementVec_(list){}autoelementsSize()//->decltype(elementVec_size()){returnelementVec_.size(