草庐IT

decltype-auto

全部标签

c++ - 使用 std::iterator traits 和 auto 在函数声明中定义一个函数

今天我尝试实现基数排序。该函数必须有两个变量:开始迭代器和结束迭代器,并且可以有第三个:一些必须返回整数类型以进行排序的函数。默认情况下,它必须是恒等函数。我的尝试看起来像(抱歉,代码看起来又长又脏,但这只是一个尝试):templatevoidradix_sort(ForwardItfirst,ForwardItlast,std::function::value_type)>get_value=[](consttypenamestd::iterator_traits::value_type&x){returnx;}){//...}get_value的返回类型当然会在编译时知道。用法应该

c++ - decltype() 可变模板基类

我在期待decltype()的地方有以下代码不工作Derived上课得到run()基类方法返回类型,因为基类没有默认构造函数。classBase{public:intrun(){return1;}protected:Base(int){}};structDerived:Base{templateDerived(Args...args):Base{args...}{}};intmain(){decltype(Derived{}.run())v{10};//itworks.Notexpectedsince//Derivedshouldnothavedefaultconstructorstd

c++ - 为什么 auto_ptr 构造不能使用 = 语法

我遇到了一个对我来说意义不大的编译器错误:#includeusingnamespacestd;auto_ptrtable=db->query("select*fromt");错误:请求从“Table*”到非标量类型“std::auto_ptr”的转换但是,以下行确实有效:auto_ptrtable(db->query("select*fromt"));构造函数的这个定义阻止它按我预期的方式工作的原因是什么?我认为初始化声明使用了构造函数。这是我的auto_ptr的构造函数(来自SGISTL):explicitauto_ptr(element_type*__p=0)throw():_M_

现代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++ - 模板类中 auto 的不完整类使用

下面的代码格式是否正确?classB;templateclassA{Bdo_f()const;friendautof(Aconst&a){returna.do_f();}//#1};classB{};templateBA::do_f()const{returnB{};}intmain(){Aa;f(a);}如果我将#1​​中的auto更改为B,我会收到不完整的类型错误消息。为gcc/clang使用auto编译DemoB失败Demo 最佳答案 [dcl.fct.def.general]/2:Thetypeofaparameterort

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);