jjzjj

decltype

全部标签

c++ - 尾随返回类型、declval 和引用限定符 : can they work together?

考虑以下示例:#includestructA{voidf(){}};structB{voidf()&{}};structC{voidf()&&{}};templateautof()->decltype(std::declval().f()){}intmain(){f();//f();//(*)f();}当用B调用时(第(*)行),代码不再编译std::declval转换T在特定情况下为右值引用类型。如果我们稍微改变一下,就会遇到相反的问题://...templateautof()->decltype(std::declval().f()){}//...intmain(){f();f()

c++ - 为什么不能形成对 'decltype(auto)' 的引用

intmain(){decltype(auto)&&a=100;}以上代码,在GCC和Clang中出错。intmain(){decltype(int)&&a=100;}此代码正确。在N4296中,在§8.3.2/6Ifatypedef(7.1.3),atypetemplate-parameter(14.3.1),oradecltype-specifier(7.1.6.2)denotesatypeTRthatisareferencetoatypeT,anattempttocreatethetype“lvaluereferencetocvTR”createsthetype“lvaluere

c++ - 使用 typeof 和 decltype 定义数组时出现编译器错误

A)这行得通:intmain(){int*a=newint[5];delete[]a;return0;}B)这个错误:intmain(){int*a=newtypeof(*a)[5];delete[]a;return0;}错误:数组下标的类型'int[int]'无效C)这行得通:intmain(){int*a=newtypeof(a)[5];delete[]a;return0;}我不明白为什么B会失败,因为扩展后语句应该如下所示:int*a=newint[5];因为typeof(*a)是int。下面是decltype的实验:D)这行不通:intmain(){int*a=newdeclt

c++ - 如何使用 decltype 获取引用的类型?

我正在CodeGearRADStudio中使用decltype编写一些代码。我已经尝试过天真的解决方案,看起来与此不同:intmain(int,char**){inti;int&ir=i;decltype((ir))ir_clone=ir;}当然,编译失败:InternalCompilerError。我宁愿怀疑该代码没有什么特别的错误,并且存在关于引用表达式的编译器错误。(顺便说一下,g++的代码没有问题并且编译得很好。)但是,这无助于解决问题,因为平台是不可协商的。如果我在上面写了decltype(ir)ir_clone=ir;/*Noextraparens*/它按预期编译和工作。然

c++ - Clang 访问修饰符顺序和 decltype

我一直在考虑创建一个同步器助手模板类,它基于HerbSutter在这个talk中的包装器类的想法。这在msvc中不起作用(除非我们删除大括号初始化)但是当大括号初始化被删除时就没问题了。在clang/gcc(ubuntu12.10,gcc4.7.2,clang(3.2)selfbuiltwithlibc++)中,private访问修饰符似乎必须出现在public之前:这看起来有点奇怪。gcc的错误是错误:“t_”未在此范围内声明clang是error:useofundeclaredidentifier't_'autooperator()(Ff)const->decltype(f(t_)

c++ - 有没有办法使用 SFINAE 检测是否未声明非模板化非成员函数?

我正在尝试回答thisquestion使用SFINAE和decltype。总而言之,发布者想要一个函数,该函数的行为取决于是否在编译单元中声明了另一个函数(声明早于或晚于所讨论的函数)。我尝试了以下方法:autosome_function_2_impl(int)->decltype(some_function_1(),void()){cout但是,我收到此错误消息:main.cpp:4:60:error:'some_function_1'wasnotdeclaredinthisscopeautosome_function_2_impl(int)->decltype(some_funct

c++ - 如何从另一个实例的类型动态实例化一个新实例? C++

我想基于另一个只能在运行时知道类型的对象在堆上创建一个新对象。举个假设的例子,假设我们有一款游戏,用户可以选择一个角色,该角色可以是巫师、战士或治疗师。计算机会创建一个匹配的非玩家角色来对抗玩家。也就是说,如果玩家选择了一个武士,计算机会生成另一个武士实例。我正在尝试使用多态性。说巫师、战士和治疗师都继承自“战斗类型”类我想做的是类似伪代码的事情:combattype*player=new(chosenatruntimetype)();//playercombattype*baseptr=newtypeid(*player);//computer-thisdoesn'twork我知道我

c++ - std::result_of 应用于 const 重载方法

如果我给typedefstd::vectorv;然后下面可以用来捕获常量迭代器的类型(另一种方法是使用v::const_iterator,但这取决于const_iterator成员类型在类中明确定义。typedeftypenamestd::result_of::typeconst_iterator;确实,我们可以检查上面的内容是否如我们所愿。static_assert(std::is_same::value);但是,我发现下面的编译器失败。typedeftypenamestd::result_of::typeiterator;编译器提示该方法被重载(通过const修饰符)并且无法明确解

c++ - 在模板函数中可以使用已删除构造函数的 decltype

据我所知,decltype不允许使用已删除的构造函数:structno_def{no_def()=delete;};voidtest(){decltype(no_def())a{};//error:useofdeletedfunction‘no_def::no_def()’}但如果我将模板设为“测试”函数,它就会编译templatevoidtest(){decltype(no_def())a{};//OK}还有它templatevoidtest(){decltype(no_def("not","defined","constructor"))a{};//OK}谁能解释一下?

c++ - decltype-specifier 表示的类型是什么类型,其表达式是类类型的临时对象的成员?

假设我们有以下声明:structS{inta;};以下简单类型说明符表示的类型是什么?是int还是int&&?decltype(S{}.a)(这个问题旨在解决C++17,但也感谢解决其他版本标准的答案。) 最佳答案 它是int。每[dcl.type.simple]/4:Foranexpressione,thetypedenotedbydecltype(e)isdefinedasfollows:[...]otherwise,ifeisanunparenthesizedid-expressionoranunparenthesizedcl