草庐IT

conversion

全部标签

c++ - 如何解决转换构造函数和普通构造函数之间的歧义?

我期待消除普通构造函数和自动转换构造函数之间的歧义。据我所知,可以通过将普通构造函数声明为explicit来部分消除这种歧义,因此编译器将避免将此构造函数用作转换构造函数,如下所示:#include#includeclassInteger{inti;public:explicitInteger(constint_i):i(_i){}//NormalconstructorInteger(constint&ii):i(ii){}//conversionconstructoroperatorint(){returnint(i);}//Auto-conversionoperatorfriend

c++ - 为什么我会收到 Wsign-conversion 警告?

我有以下代码:templatestructwrapper{Tt;operatorT(){returnt;}Tget(){returnt;}};intmain(){inta[10];int*x=a;wrappery{2};std::cout当我使用-Wsign-conversion在gcc上编译它(在7.3.0和8.2.0上测试)时,我收到“警告:从‘longunsignedint’转换为‘longint’可能改变结果的符号”。如果y的类型为longunsignedint,则不会出现警告。此外,当我显式调用y.get()时,也没有警告:std::cout为什么会这样?是否有一些特殊的指针

c++ - C4244 : '+=' : conversion from 'std::streamsize' to 'size_t' , 可能丢失数据

我已将我的VC++项目从VS2008迁移到VS2013并收到一些警告,例如:C4244:'+=':conversionfrom'std::streamsize'to'size_t',possiblelossofdata.如何解决这些类型的警告? 最佳答案 在MSVC2013中std::streamsize是:typedef_Longlongstreamsize;typedef_LONGLONG_Longlong;#define_LONGLONG__int64size_t是:typedefunsigned__int64size_t;因

c++ - NULL指针转换

C++03$4.10-"Theconversionofanullpointerconstanttoapointertocv-qualifiedtypeisasingleconversion,andnotthesequenceofapointerconversionfollowedbyaqualificationconversion(4.4)."这是我的理解intmain(){charbuf[]="Hello";charconst*p1=buf;//2stepconversionprocess//C1:char[6]tochar*//C2:char*tocharconst*(qualif

c++ - G++ 警告 : narrowing conversion of

我正在尝试从sourceforge编译hosts3d,它确实编译但生成了几个缩小错误。我不知道如何解决这个问题,但我们将不胜感激任何帮助。我怀疑我可以下载以前版本的编译器,我可能最终会这样做,但现在……c++11g++-Wall-O2-c-osrc/glwin.osrc/glwin.cppsrc/glwin.cpp:成员函数中'intMyGLWin::AddInput(int,int,unsignedint,int,constchar*,bool)':src/glwin.cpp:983:147:warning:narrowingconversionof'max'from'int'to'

比较运算符重载与转换运算符的 C++ 优先级

考虑以下程序:#includeusingnamespacestd;classFoo{public:intk;operatorint(){cout当USE_COMPARE定义,if(f的比较将使用比较运算符重载。如果USE_COMPARE未定义,它将转换f来自Foo至int,然后进行整数比较。在我看来,比较运算符重载的优先级高于转换运算符。任何人都可以从C++标准的角度确认这一点吗?但我认为比较运算符应该具有优先权是自然的。但请从C++标准的角度回答问题。谢谢。 最佳答案 13.3.3.2/2Whencomparingthebasic

c++ - 用户自定义转换的第二个标准转换顺序

我对标准转换序列术语有误解。我遇到了以下引用N3797§8.5.3/5[dcl.init.ref]:—Iftheinitializerexpression—isanxvalue(butnotabit-field),classprvalue,arrayprvalueorfunctionlvalueand“cv1T1”isreference-compatiblewith“cv2T2”,or—hasaclasstype(i.e.,T2isaclasstype),whereT1isnotreference-relatedtoT2,andcanbeconvertedtoanxvalue,clas

c++ - 模板参数推导是否考虑了返回类型?

我正在阅读“C++模板:完整指南(第二版)”,第10页。根据本书,模板参数推导不考虑返回类型。TemplateDeductioncanbeseenaspartofoverloadresolution.Aprocessthatisnotbasedonselectionofreturntypes.Thesoleexceptionisthereturntypeofconversionoperatormembers任何在推导中考虑返回类型的示例都会有所帮助。 最佳答案 structA{intvalue;//conversionoperato

c++ - 总是使用构造函数而不是显式转换运算符

我有以下类(class):templateclassFoo{public:Foo(T1*obj):obj(obj){}templateFoo(constFoo&other):obj(other.obj){}templateexplicitoperatorFoo(){returnFoo(static_cast(obj));}T1*obj;};第二个构造函数的目的是从Foo隐式转换至Foo如果从X*进行隐式转换,则允许至Y*是允许的。转换运算符允许从Foo进行显式转换至Foo使用来自X*的显式转换至Y*.但我注意到转换运算符从未被使用过。编译器总是使用第二个构造函数,即使我进行显式转换也是

C++ 重载歧义 : conversion versus promotion with primitive types

在这段代码中:voidf(floatf,longinti){cout有一个歧义。Checkitout!.但是,第二个参数是有符号整数。将int绑定(bind)到longint参数需要提升,但对于float,则需要转换。由于第一个参数是关于两个重载的完全匹配,所以它不算数。但是关于第二个参数,它在第一次过载(提升)上的排名优于在第二个(转化)上的排名。为什么会出现解析歧义,而不是选择第一个重载? 最佳答案 int到long是一个转换。short到int是一种提升。(有关积分促销的完整列表,请参阅[conv.prom]。)同理,floa