我不确定是否完全理解[dcl.type]/4.3:Foranexpressione,thetypedenotedbydecltype(e)isdefinedasfollows:[...](4.3)otherwise,ifeisanunparenthesizedid-expressionoranunparenthesizedclassmemberaccess,decltype(e)isthetypeoftheentitynamedbye.Ifthereisnosuchentity,orifenamesasetofoverloadedfunctions,theprogramisill-fo
当我尝试在私有(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++0x的练习,我尝试创建一个C++类来包装一些模板化类型的指针:templateclassWrapper{T*t;/*...*/};在Wrapper类内部,我想公开T可能通过Wrapper类实现的任何重载运算符。包装器本身只是将函数调用转发给底层的t对象。templateautooperator+(U&u)->decltype(*t+u){return*t+u;}要注意的是,我不希望Wrapper公开T可能无法实现的运算符。例如,如果T没有实现operator+,那么Wrapper也不应该公开operator+。在operator+(和任何二元运算)的情况下,一切正常,因为
在下面的示例中,如何获取成员函数的返回类型?templateclassMyClass{typedefdecltype(mygetter.get())gotten_t;...};当然,问题是我在定义MyClass时没有“mygetter”对象。我想做的是:我正在创建一个缓存,它可以使用getter返回的任何内容,因为它是键。 最佳答案 我不太确定你想要什么,但看起来mygetter应该只是Getter类型的任何对象。使用std::declval就可以得到这样一个对象,没有别的(你只能用它来进行类型推导)typedefdecltype(
C++标准对“xvalues”的描述如下(N4762§7.2.1.4):Anexpressionisanxvalueifitis:-...-aclassmemberaccessexpressiondesignatinganon-staticdatamemberofnon-referencetypeinwhichtheobjectexpressionisanxvalue考虑以下代码片段(使用Boost打印表达式的类型):#include#includeusingboost::typeindex::type_id_with_cvr;structX{intvar;}x;intmain(){a
此刻,我是usingthismethodtocheckifaclasshasamethodwithaspecificsignature.参加后WalterE.Brown'smetaprogrammingCppCon2014talk,我开始怀疑void_t可以在这种特殊情况下使用,使代码更清晰、更具可读性。但是我在考虑void_t方面遇到了麻烦-到目前为止我明白void_t可以帮助我在编译时确定表达式是否有效。例子:templatestructhas_type_data_member:false_type{};templatestructhas_type_data_member>:tru
这个问题非常类似于:“Extractjusttheargumenttypelistfromdecltype(someFunction)”。我不确定那里的答案是否适合我的意图。我希望能够创建一个模板函数,该函数根据函数指针模板参数(whiSTLes)的类型推断其运行时参数的类型。举一个示例用例,假设我想使用加载了LD_PRELOAD的垫片库来检测直接CPOSIX文件I/O。我可以为fopen、fread、fwrite、fclose编写单独的包装器……如果所有这些包装器都做类似的事情,如果我可以定义一个捕获常见行为的模板不是很好吗?不使用模板的部分示例演示了涉及多少样板文件:extern"
通过使用ExpressionSFINAE,你可以检测是否有一些operator或operation是否支持。例如,templateautof(T&t,size_tn)->decltype(t.reserve(n),void()){t.reserve(n);}我的问题是decltype中的t.reserve(n)是否被执行?如果是,是否意味着t.reserve(n)被执行了两次,一次在decltype中,另一次在函数体内?如果不是,是否只是在编译期间检查验证?但是为什么不执行,我以为逗号分隔的表达式列表中的所有表达式都会被执行。 最佳答案
使用以下宏:#defineASSERT_IF_TEMP(expr)static_assert(?,"Istemporary!");问号应该填什么? 最佳答案 首先我们应该澄清一下:“临时”是什么意思?许多人在说临时时有不同的意思。从技术上讲,int()不是临时的,但大多数人会将它们包括到他们自己的那个术语的含义中。从技术上讲,给定std::strings;,那么move(s)也不是临时的,但您可能希望将其与您的宏一起对待。我上面提到的第一种“临时变量”实际上是“纯右值表达式”。这些是std::string("foo")或int()之
下面的代码允许我更改*p2的值,即使p2是用const声明的。int*p1;constdecltype(p1)p2=newint(100);*p2=200;cout但是,如果我使用“int*”而不是“decltype(p1)”,那么编译器会标记错误。constint*p2=newint(100);*p2=200;cout我正在使用g++(Ubuntu4.8.2-19ubuntu1)4.8.2。应用于指针变量时,decltype是否忽略const说明符? 最佳答案 constdecltype(p1)p2表示int*constp2。这意