草庐IT

implicit-conversion

全部标签

c++ - 与枚举的隐式转换

这是一个基本问题,我希望轻松地谷歌一下,但没有找到答案。假设我有一个枚举:enumabc{a,b,c};支持哪些隐式转换?编译器之间是否有任何编译器扩展或不同行为?我问的是关于到枚举的隐式转换:enumabctest=(**whichtypecanappearhere?**);以及从枚举的隐式转换:(**whichtypecanappearhere?**)test2=test;我想知道C和C++的答案。 最佳答案 正如引用所说:Valuesofunscopedenumerationtypeareimplicitly-converti

c++ - 运算符 T() 未在赋值中使用

我对此有点困惑。假设我有一个辅助类DataclassData{public:Data(constQVariant&value):m_Variant(value){}operatorQString()const{returnm_Variant.toString();}private:QVariantm_Variant;};然后当我这样做时:Datad("text");QStringstr=d;//strbecomes"text"它有效,但当我继续这样做时:Datad2("text2");str=d2;//doesnotcompile提示失败:ambiguousoverloadfor'op

c++ - std::map 和 -fno-implicit-templates

我正在尝试使用g++4.4进行编译并链接一个使用STL的简单程序。我正在尝试使用-fno-implicit-templates来做到这一点,因此必须显式实例化所有模板。我不明白为什么此代码有效:#include//templateclassstd::map;templateclassstd::_Rb_tree,std::_Select1st>,std::less,std::allocator>>;intmain(){std::maptable;return0;}我希望这个程序需要这一行:templateclassstd::map;,但是该行不会使程序链接。std::_Rb_treeli

c++ - 构造函数转换

类X->通过两种方式转换为Y1)构造函数,以及2)通过转换函数。我了解单参数构造函数用于转换。在规范中:Animplicitly-declaredcopyconstructorisnotanexplicitconstructor;itmaybecalledforimplicittypeconversion.问题:那么,这意味着不仅单参数构造函数用于转换,而且复制构造函数?。如果是这样,它使用什么场景?任何示例代码片段?如果问题很基础,请多多包涵。 最佳答案 复制构造函数不是显式构造函数,因此将尽可能使用它。复制构造函数只会从相同的类

c++ - boost::可选的函数返回

我正在审查一些生产代码,其中一个函数说它将返回一个boost::optional,但它只返回一个double:例如boost::optionalFoo(){doublea=1.0;doubleb=2.0;returna+b;}这种风格是否可以接受/是否存在不安全的情况? 最佳答案 这是我喜欢使用的样式。返回的double将被隐式转换为boost::optional,它已被设置并包含该double的值。我想不出任何不安全的情况。编辑:当optional持有的类型为bool时有一些注意事项-请参阅文档。此转换将使用此boost::opt

c++ - 确定与 std::function<R(T1,T2)> 兼容的函数类型集的规则?

假设我有这个,std::functionfs;那么我如何确定fs的函数集(或函数对象)可以初始化吗?以下哪些是允许的,哪些不是:std::functionfs=[](int,int){returnint(10);};std::functionfs=[](char,char){returnchar(10);};std::functionfs=[](int,short){returnint(10);};std::functionfs=[](double,int){returnfloat(10);};std::functionfs=[](int,wchar_t){returnwchar_t(

c++ - 在 C : Derived to base conversions 中包装 C++

我正在将一个简单的C++继承层次结构包装到“面向对象的”C中。我试图弄清楚在将指向C++对象的指针视为指向不透明C结构的指针时是否存在任何问题。特别是在什么情况下派生到基的转换会出现问题?类本身比较复杂,但层级较浅,仅采用单继承://AbaseclasswithlotsofimportantsharedfunctionalityclassBase{public:virtualvoidsomeOperation();//Moreoperations...private://Data...};//OneofseveralderivedclassesclassFirstDerived:pub

c++ - 隐式转换为模板

我下面的示例表明,从非模板类型到模板类型的隐式转换不会像仅涉及非模板类型的那些那样无缝地工作。有没有办法让它们继续工作?例子:structpoint;templatestructvec{vec(){}//...};templatestructvec{vec(){}vec(constpoint&p){/*...*/}//Conversionconstructor//...};structpoint{operatorvec(){returnvec(/*...*/);}//Conversionoperator};templatevecfoo(veca,vecb){returnvec(/*..

c++ - 为什么输出带有转换运算符的类不适用于 std::string?

Thisworks,printing1:#includestructInt{inti;operatorint()constnoexcept{returni;}};intmain(){Inti;i.i=1;std::cout然而,thisfailstocompileonGCC4.8.1:#include#includestructString{std::strings;operatorstd::string()const{returns;}};intmain(){Strings;s.s="hi";std::cout以下是错误的相关部分:error:nomatchfor‘operators

c++ - 警告 C4244 : 'argument' : conversion from 'double' to 'const int' , 可能丢失数据

我正在定义“*”运算符以使用“NumericArray”类模板。代码如下:templateNumericArrayNumericArray::operator*(constT&factor)const{NumericArraynewArray(Size());for(inti=0;i当我尝试将类型为“int”的“NumericArray”(NumericArray)与“*”运算符一起使用时,当“factor”参数为double时:intArray1=intArray1*2.5;我收到以下编译器警告:warningC4244:'argument':conversionfrom'doubl