草庐IT

type-equivalence

全部标签

C++11/14 : How to remove a pointer-to-member from a type?

在C++11或C++1y/14中:给定一个TC::*形式的指向成员类型的值,我想得到指向类型例如:#includeusingnamespacestd;structT1{staticvoidf(){coutstructV;templatestructV{staticconstexprT1U1::*pm=&U1::x;};templatestructV{staticconstexprT2U2::*pm=&U2::x;};templatevoidf(Wpm){typedef???T;T::f();}intmain(){f(V::pm);f(V::pm);}有没有办法做到这一点?上面的???是

c++ - CTRP 派生类中没有名为 'type' 的类型

我一直在试验CuriouslyRecurringTemplatePattern对于通用的单参数仿函数,有两种实现:一种使用有效的模板模板参数,另一种我尝试在接口(interface)类中访问派生的Functor::type。在后一个示例中,编译器(gcc5.4.0)报告error:notypenamed'type'in'structCube'templateclassFunctor>classFunctorInterface_1{private:constFunctor&f_cref;public:FunctorInterface_1():f_cref(static_cast&>(*t

c++ - 错误 : Expression must have integral or unscoped enum type

#include#include#include#include#includeusingnamespacestd;intmain(){floatsize;floatsumNum=0;floatmaxNum,minNum;floatmean;floattotalDev=0;floatdevSqr=0;floatstdDev;//Createauserinputsizestd::cout>size;float*temp=newfloat[size];//Gettinginputfromtheuserfor(intx=1;x>temp[x];}//Outputofthenumbersins

c++ - 错误 : Qualifiers dropped in binding reference of type x to initializer of type y

为什么下面会抛出这个错误:IntelliSense:qualifiersdroppedinbindingreferenceoftype"string&"toinitializeroftype"conststring".hclassA{public:wstring&GetTitle()const;private:wstringtitle;};.cppwstring&GetTitle()const{returnthis->title;}如果我删除const词,它就会停止提示,但我从未对变量进行任何更改? 最佳答案 通过返回对类成员的非c

c++ - 为什么 C++1* 仍然需要 template 关键字来代替 Full Duck Typing

很多年前,(至少对我而言)静态C++多态性似乎是连贯的。Python等语言依赖ducktyping,你有:deffn(foo,bar):foo.baz(bar.bo())当时的想法是,如果它适本地“嘎嘎叫”,那么语言就没问题。相反,在C++中,您必须解释它是什么“动物”:voidfn(foo_typefoo,bar_typebar);对于“家庭王国”,您明确需要使用template关键字:templatevoidfn(Foofoo,Barbar);具有像auto...()->decltype这样的新功能返回类型,尤其是genericlambdas,似乎有一些更像是非模板Python类的

c++ - 定义两个变量 : Is "Type a(arg), b(arg);" completely equivalent to "Type a(arg); Type b(arg);"?

假设我想定义两个{Type}类的变量。构造函数采用1个参数。下面两种方式是否完全等价(编译成相同的目标代码)?Typea(arg),b(arg);和Typea(arg);Typeb(arg);这个问题是在我阅读了一个讨论异常安全的页面后出现的---http://www.gotw.ca/gotw/056.htm有一个指南“在其自己的代码语句中执行每个资源分配(例如,新的),立即将新资源提供给管理器对象。”它举了一个例子:以下代码段是安全的auto_ptrt1(newT);auto_ptrt2(newT);f(t1,t2);但是下面这行是不安全的f(auto_ptr(newT),auto_

c++ - "const T& operator[](size_type i)"中的 const 有什么用?

我在一本书http://www.acceleratedcpp.com/中发现了这个有趣的行-资源-第11章-Vec.h(我是一个std::vector翻版)而且我真的不明白这个版本的运算符有什么用。为什么要定义此运算符的两个版本(常量和非常量)?我什至试过了,在我看来,非常量版本一直被调用......你能解释一下吗?#include#include#include#includeusingnamespacestd;templateclassVec{public:typedefT*iterator;typedefconstT*const_iterator;typedefsize_tsiz

c++ - 错误 C2678 : binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)

我正在尝试编译以下代码:#include#include#includetypedefboost::geometry::model::d2::point_xyPoint;typedefstd::pairVector;booloperator==(constPoint&p1,constPoint&p2){returnp1.x()==p2.x()&&p1.y()==p2.y();}intmain(){Vectorvec1(Point(0,0),Point(1,1));Vectorvec2(Point(0,0),Point(1,2));std::coutVS2012C++编译器返回以下编译错

c++ - 海湾合作委员会 : Unscoped enumeration type give an ambiguity error

在下面的代码中,我定义了一个unscopedenumeration为longlong类型。该程序在Clang上运行良好。但是GCC编译器给出了一个歧义错误。#includeenum:longlong{Var=5};voidfun(longlongll){std::coutGCC产生的错误:main.cpp:Infunction'intmain()':main.cpp:17:12:error:callofoverloaded'fun()'isambiguousfun(Var);^main.cpp:5:6:note:candidate:voidfun(longlongint)voidfun

c++ - 自动返回类型扣除警告 : why do we need decltype when return defines the type anyway?

这是一个关于elementsSize()成员函数做什么的问题,关于自动返回类型推导:#include#includetemplateclassElementVector{std::vectorelementVec_;//Otherattributes.public:ElementVector()=default;ElementVector(conststd::initializer_list&list):elementVec_(list){}autoelementsSize()//->decltype(elementVec_size()){returnelementVec_.size(