jjzjj

decltype

全部标签

c++ - 用 decltype 解释这段代码

([]()->decltype(std::cout打印Hello,world!。我根本不明白这里发生了什么。有人可以用简单的语言向我解释一下吗? 最佳答案 ([]()->decltype(std::cout这将创建一个lambda(就地函数),其返回类型与表达式std::cout相同有-那是std::ostream&.[]启动lambda,()是一个空参数列表,->在返回类型之前,并且decltype(X)相当于表达式X的类型。那么函数体:{returnstd::cout然后函数被调用...输出“Hello”并返回流....())最

c++ - 递归可变参数函数模板的返回类型的decltype

给定以下代码(取自here):#include#include#include#include#include#includetemplatestructcompose_impl{compose_impl(Fs&&...fs):functionTuple(std::forward_as_tuple(fs...)){}templateautoapply(std::integral_constant,Ts&&...ts)const{returnapply(std::integral_constant(),std::get(functionTuple)(std::forward(ts)...

c++ - 获取模板化对象方法的返回类型

说我有:templatestructFoo{T&func();};然后我实现了一个Foo:Foobar现在我想获得bar.func()的返回类型.我一直在努力强制result_of与我合作但无济于事。我真正想要的是能够做到result_of_t并完成它,但我想这要困难得多?我应该如何获得这种返回类型?编辑:我希望在不考虑bar的情况下实现这一目标被宣布。也就是说,我只想能够通过bar.func进入result_of或类似的并输出返回类型。 最佳答案 std::result_of实际上使用起来很烦人。它的语法是:result_of在哪

c++ - 在 C++ 中使用 decltype 声明指向方法的指针

clang和gcc之间有一些区别。其中之一是他们如何处理指向方法的指针。给定以下代码:templatevoidstore_method(T_Class*object,T_Ret(T_Class::*method)(Args...args));classSomeObjectWithPotentiallyLongName{intcommonly_used_method(intvar);voidregister_method(){/*Thecodebelowisokayforgccwith-std=gnu++11.Butclang*says:*'referencetonon-staticme

C++0x decltype 推导成员变量常量失败

考虑以下代码:templateclassB{};templateBf(T&t){returnB();}classA{classC{};Cc;public:A(){}decltype(f(c))get_c()const{returnf(c);}};intmain(){Aa;a.get_c();}当我尝试编译它时,出现错误:test.cpp:Inmemberfunction'BA::get_c()const':test.cpp:31:46:error:conversionfrom'B'tonon-scalartype'B'requested似乎在decltype中,编译器不知道这是一个co

c++ - 使用 decltype 作为尾随返回类型的正确方法

我经常看到这种形式的例子:templateautoadd(T&&t,U&&u)->decltype(std::forward(t)+std::forward(u)){returnstd::forward(t)+std::forward(u);}但我敢说这是更好更正确的方法:templateautoadd(T&&t,U&&u)->decltype(t+u)//noforwardinghere{returnstd::forward(t)+std::forward(u);}为什么?首先,此示例中的decltype仅需推导返回类型,因此(t+u)不是(std::forward(t)+std::

c++ - mingw g++ 4.7.2 中未声明的 decltype

出于某种原因,当尝试在mingw上用G++编译以下代码时#include#include#includeintmain(intargc,char**argv){std::strings("Hello,World!");decltype(s.size())punct_cnt=0;for(autoc:s){if(ispunct(c))++punct_cnt;}std::cout出现以下错误test.cpp:Infunction'intmain(int,char**)':test.cpp:9:23:error:'decltype'wasnotdeclaredinthisscopetest.c

c++11 decltype 返回引用类型

我有点困惑为什么在某些情况下使用逗号运算符的decltype会返回引用类型。例如,在这段代码中:inti=101;decltype(1,i)var=i;var=20;printf("%d\n",i);//willprint20在这里,var是int&而不是int,但是如果我将第二行替换为:decltype(i)var=i;它将返回int!谁能解释一下? 最佳答案 decltype是特殊情况,用于未加括号的id-expression以给出实体的类型,没有引用限定[dcl.type.simple]:4-Thetypedenotedbyd

c++ - 成员函数中的 decltype(auto) 忽略无效主体,decltype(expr) 失败

我有一个简单的模板化包装器结构,其成员函数在其模板类型的对象上调用.error()。templatestructWrapper{Tt;decltype(auto)f(){returnt.error();//calls.error()}};如果我用一个没有error()成员函数的类型实例化它,只要我不调用它就没问题。这就是我想要的行为。Wrapperw;//noproblemhere//w.error();//uncommentedcausescompilationfailure如果我使用我认为是具有尾随返回类型的语义等价物,它会在变量声明上出错templatestructWrapper

c++ - const decltype(*std::begin(container))& val 不会使 val const?

这段代码:std::vectorints(5,1);std::for_each(ints.begin(),ints.end(),[](constdecltype(*std::begin(ints))&val){val*=2;});在VisualStudio2010中编译和运行得很好,并且修改容器中的每个值,就像没有const关键字一样。这是编译器中的错误吗,因为预期的行为是val是不可修改的?(换句话说,我希望它不会编译,但它会编译)更新:std::for_each(ints.begin(),ints.end(),[](conststd::remove_reference::type&