草庐IT

decltype

全部标签

现代C++中的decltype(auto):理解与运用

在C++14及以后的版本中,decltype(auto)成为了编译器和开发者的得力助手。它主要用于在编译时推断表达式的类型,并保证推断出的类型在上下文中是有效的。decltype(auto)比传统的decltype更强大,因为它能够处理更为复杂和动态的类型。decltype(auto)的工作原理decltype(auto)会根据初始表达式的类型进行推断,并在必要时对推断出的类型进行调整,以确保类型安全和一致性。例如,如果初始表达式是一个数组,decltype(auto)将推断出数组的元素类型;如果初始表达式是一个函数,decltype(auto)将推断出函数的返回类型。decltype(aut

C++11:重载无法解析递归 decltype

在下面的代码中,我尝试构建一个类型格。例如,在float和int之间,将结果提升为float:floatjoin(floatf,int){returnf;}floatjoin(floatf,float){returnf;}然后我引入一个wrapper类型:templatestructwrapper{usinginner_t=Inner;inner_tvalue;};join操作的行为非常自然:templateautojoin(constwrapper&w1,constwrapper&w2)->wrapper{return{join(w1.value,w2.value)};}它也可以用“

c++ - 本地结构中是否允许成员声明 `decltype(name) name;`,其中第一个名称指的是封闭范围?

例子:intmain(){inta=0;structX{decltype(a)a;};return0;}decltype(a)引用了main中的局部a,而它声明的成员同名。Clang编译没有任何问题,MSVC14也是如此。G++提示它,添加-fpermissive让它通过prog.cc:6:21:error:declarationof'intmain()::X::a'[-fpermissive]decltype(a)a;^prog.cc:3:9:error:changesmeaningof'a'from'inta'[-fpermissive]inta=0;哪种行为符合标准?

c++ - 成员函数的decltype

classA{intf(intx,intj){return2;}decltype(f)*p;};给我错误:error:decltypecannotresolveaddressofoverloadedfunction我不明白为什么这个错误甚至是在谈论重载函数。同样,我认为也许我需要使用作用域运算符来访问该函数:classA{intf(intx,intj){return2;}decltype(A::f)*p;};这仍然给我一个错误,但描述更清晰:error:invaliduseofnon-staticmemberfunction'intA::f(int,int)'为什么不允许我使用decl

具有 decltype : substitution failure becomes an error? 的 C++ SFINAE

此代码有效://CodeA#include#include#includeusingnamespacestd;templatestructS{template()))>::value>::type>S(Iter){coutv;Ss1(v.begin());//stdout:S(Iter)Ss2(1);//stdout:S(int)}但是下面这段代码不起作用。在下面的代码中,我只想继承std::enable_if,所以类is_iter_of将具有成员typedeftype如果选择的版本std::enable_if具有成员typedeftype。//CodeB#include#includ

c++ - `auto` 的 ref- 和 cv-stripping 属性。

我学会了以这种方式使用auto声明一个变量autovar=expr;基本上就像获取expr的类型并从中剥离&/&&-references和所有顶级常量和volatile。这是否意味着上面的行完全等同于下面的行?std::remove_cv::type>::typevar=expr; 最佳答案 不,那不是真的。autovar=expr;更像是传递expr按值(value)。intx[1];autoy=x;这使得y一个int*.主要是autox=expr;表现得像模板类型推导:templatevoidf(T);intx[1];f(x);

c++ - sfinae 使用 decltype 检查静态成员

我写了下面的代码来尝试检测一个类型是否有一个静态成员变量。不幸的是,它总是返回变量不存在。有人能告诉我哪里错了吗?我正在使用g++4.7.1。#include#include#includeusingnamespacestd;templateclasshas_is_baz{template::value>::type...>staticstd::true_typecheck(int);templatestaticstd::false_typecheck(...);public:staticconstexprboolvalue=decltype(check(0))::value;};st

c++ - 如何使用隐式模板类型推导

我正在尝试编写一个模板来在编译期间计算一个数的幂(我不是模板元编程专家,因此欢迎任何评论)。下面是代码:templatestructPow{staticconstexprTresult=X*Pow::result;};templatestructPow{staticconstexprTresult=1;};templatestructPow{staticconstexprTresult=X;};我需要这样调用:Pow::result问题:有没有什么方法可以编写帮助程序模板以便调用跳过decltype?例如:Pow::result我已阅读以下内容,但到目前为止我还没有看到答案(似乎恰恰相

c++ - 继承重载成员函数指针的非类型模板形参

背景考虑以下代码:structA{//aclasswewanttohidefromtheclientcodeintf();intf(char);intg();};structB:A{};//theinterfaceclass//clientcode:////It'snaturaltoassumeamemberfunctionofTshouldhave//atypeofint(T::*)(),right?templatestructF{};//compiles,ofcourseF{};//doesn'tcompile,because&B::gisnotoftype`int(B::*)()

c++ - 编译器无法推断返回类型?

我正在尝试在自动函数上使用decltype关键字:structThing{staticautofoo(){return12;}usingtype_t=decltype(foo());};我得到以下错误(gcc7.4)::6:25:error:useof'staticautoThing::foo()'beforedeductionof'auto'decltype(foo());^:6:25:error:useof'staticautoThing::foo()'beforedeductionof'auto'为什么编译器还没有推导出返回类型? 最佳答案