草庐IT

decltype

全部标签

c++ - decltype(auto) foo() 在没有任何警告的情况下返回本地引用

在使用现代C++中的示例时,我编写了以下代码。#include#includestaticintcount=0;classCounter{public:Counter(){++count;};Counter(Counter&r){++count;};Counter(Counter&&r){++count;};~Counter(){--count;};voidfoo(){};};decltype(auto)foo_warn(){Counterc;return(c);//Warningaboutreturninglocalreference}decltype(auto)foo_no_war

c++ - decltype( (A{}.int_member) ) 的正确结果是什么?

给定类型A的定义:structA{inti;};根据规范[expr.ref](我使用了n4618):(ifE2isnon-reference,)...IfE1isanlvalue,thenE1.E2isanlvalue;otherwiseE1.E2isanxvalue...显然A{}.i是xvalue;还考虑到[dcl.type.simple]:(fordecltype(e),)—...ifeisanunparenthesizedid-expressionoranunparenthesizedclassmemberaccess...—otherwise,ifeisanxvalue,de

c++ - 带有 decltype : bug in clang or gcc? 的 sfinae

Clang-3.2可以编译并且代码按预期运行:structhave_f{intf(inti){return10;}};structempty{};templatestructouter{Tt;//ifThavef(),defineouter_f()templateintouter_f(inti){returnt.f(i);}};intmain(){outero1;outero2;//tosilenceunusedvarwarningreturno1.outer_f(10)+sizeof(o2);}任何版本的GCC拒绝:t.cc:13:6:error:‘structempty’hasno

c++ - 在指向成员的指针声明中使用 'decltype' 是否有效?

想象一下,出于某种奇怪的原因,我写下了这个:intmain(){structS{inti;}var;intdecltype(var)::*pint=&decltype(var)::i;}GCC尽管Clang似乎编译得很好失败并出现一些不确定的语法相关错误消息。那么神圣的ISO文件对此有何评论-这是否有效? 最佳答案 这实际上是一个knownbug在Clang中。代码有效。N4140[dcl.mptr]/1:InadeclarationTDwhereDhastheformnested-name-specifier*attribute-

c++ - 存储和重新使用 decltype 值?

如果我有模板:templatestructItem{T_value;};然后我可以做://...Itemx={42};//declaredasanint//...decltype(x._value)y=20;//'y'isalsoanint但是否可以存储decltype到一个变量以便以后可以使用它?为什么?我想将项目的值存储为指针。类似于std::vector但由于它们是模板,我必须将它们存储为指向void的指针:std::vectoris;is.push_back(newItem());is.push_back(newItem());is.push_back(newItem());这

c++从decltype返回类型中删除noexcept

#include#include#includestd::functionmockbind=::bind;intmain(){}上面的代码适用于我编译的大多数平台。但是在使用g++-7的ubuntu14.04上我得到一个错误:X.cpp:7:65:error:variable‘std::functionmockbind’hasinitializerbutincompletetypestd::functionmockbind=::bind;^~~~~~~~现在,如果我手动更改mockbind的类型std::functionmockbind=::bind;不出所料,我得到了同样的错误:现在

c++ - 为什么字符串文字上的 decltype 不产生数组类型?

该标准在§2.13.5/8中将字符串文字的类型定义为:OrdinarystringliteralsandUTF-8stringliteralsarealsoreferredtoasnarrowstringliterals.Anarrowstringliteralhastype“arrayofnconstchar”,wherenisthesizeofthestringasdefinedbelow,andhasstaticstorageduration(3.7).因此,例如,"sss"应该有一个类型charconst[4](除非我没看错)。但是这个简单的片段:std::cout::valu

c++ - 一般开始,加上 decltype 考虑本地 using-declaration

C++0x的ranged-for循环有一个处理数组的特殊异常(exception)(FDIS§6.5.4),并且有两个函数,std::begin和end,它们被重载以处理数组或选择begin/结束方法。这使我相信可以编写一个接受通用序列的函数来匹配范围for循环的行为:templatevoidf(C&c){usingstd::begin;usingstd::end;do_something_with(begin(c),end(c));}如果C的命名空间中有一个“更具体”的开始/结束,它将通过ADL选择,否则代码“默认”为std::begin/end。但是,ranged-for有这个特殊

c++ - lambda : GCC bug and/or Clang bug? 中捕获变量的 decltype()

我检查了GCCbuglist和Clangbuglist并且还没有看到任何相关内容。ThisWandboxlink显示一些C++11/C++14代码为各种类型的x执行decltype(x)和decltype((x))被lambda捕获。GCC和Clang对这段代码给出了不同的答案。如果有的话,哪一个是正确的?这是有问题的片段://insidemain()inti=42;int&j=i;[j=j](){static_assert(std::is_same::value,"");//Astatic_assert(std::is_same::value,"");//B}();[=](){sta

c++ - 使用 decltype 将其转换为 const

我正在尝试解决一个问题,其中decltype会大大简化事情,但我在*thisdecltype遇到了问题并添加一个const限定符。下面的示例代码演示了这个问题。#includestructFoo{voidbar(){static_cast(*this).bar();}voidbar()const{std::cout代码在MSVC2010中编译,但执行递归直到发生堆栈溢出。Ideone报告编译器错误prog.cpp:Inmemberfunction'voidFoo::bar()':prog.cpp:7:38:error:'const'qualifierscannotbeappliedto