草庐IT

c++ - C++ 中的隐式转换

给定以下代码,为什么编译器在构造Bar时不解析隐式转换?也就是说,构造Foo就像构造a一样,然后(应该)用于构造Bar?#includeclassImplicitlyConvertToChar{public:ImplicitlyConvertToChar(constchar*a_char):m_string(a_char){}ImplicitlyConvertToChar(constchar*a_char,size_ta_end):m_string(a_char){}templateImplicitlyConvertToChar(T_Stringconst&a_string):m_st

c++ - 来自 std :unique_ptr to bool 的隐式转换错误

我正在使用Allegro创建一个简单的游戏。当我尝试验证指向显示器的指针不为空时,我收到编译器错误提示errorC2664:'voidvalidate(bool,std::string)':cannotconvertargument1from'std::unique_ptr>'to'bool'这是我的代码#include#include#include#includeusingnamespacestd;constintWIDTH=512;constintHEIGHT=512;voidvalidate(boolptr,stringerrorMessage){if(!ptr){cerrdi

c++ - 隐式转换为引用

我有:structvec{__m128m128;inlinevec(__m128m128):m128(m128){}}所以现在__m128可以隐式转换为vec,但是当我使用它时,如下所示:voiddoStuff(vec&v){*stuffbedoing*}doStuff(_mm_set1_ps(1.0f));//mm_set_psreturns__m128我收到一条错误消息:Can'tconvertfrom__m128to&vec问题是什么以及如何解决? 最佳答案 doStuff接收对非常量vec的引用。非常量引用不能像函数调用的结

c++ - 流到 bool 的隐式转换

拿这个玩具代码:#include#includeintmain(){std::ifstreamis;//performread//...if(!is)//worksstd::cout你会得到以下反直觉的结果:if(!is)编译,if(is==false)给出errorC2678:binary'==':nooperatorfoundwhichtakesaleft-handoperandoftype'std::ifstream'(orthereisnoacceptableconversion)(对于VS2015-gcc和clang中的类似错误)。标准说(根据thisanswer):Vali

c++ - 为什么从初始化列表中初始化 vector 时不使用 move 构造(通过隐式构造函数)

为了演示move语义,我编写了以下示例代码,其中包含来自int的隐式构造函数。structC{inti_=0;C(){}C(inti):i_(i){}C(constC&other):i_(other.i_){std::cout和autovec2=std::vector{1,2,3,4,5};cout有输出Acopyconstructionwasmade.1Acopyconstructionwasmade.2Acopyconstructionwasmade.3Acopyconstructionwasmade.4Acopyconstructionwasmade.5reversingAmov

c++ - gcc:警告:大整数隐式截断为无符号类型

#includeintmain(){unsignedcharc;c=300;printf("%d",c);return0;}输出是可预测的还是未定义的? 最佳答案 很抱歉第一个回答,这里是C++标准的解释:)Istheoutputinanywaypredictableoritsundefined??这是可以预见的。这段代码有两点需要注意:一、unsignedchar类型不能赋值:unsignedcharc;c=300;3.9.1Fundamentaltypes(Page54)Unsignedintegers,declaredunsi

c++ - 在 C++ 中,在创建对象时,隐式使用 new 吗?

当我创建一个类的对象时,比如说,classA{public:A(){}};Aa;是否只调用了构造函数?还是隐式使用了new运算符?就像我们必须做的那样A*b=newA();另外,a和b在内存中的存储位置?栈还是堆? 最佳答案 第一种情况,如果a不是全局变量,那么它会被放入栈中,而b会被放入堆中。在第一种情况下,只调用了构造函数。new永远不会被调用,除非您像第二种情况那样明确地执行它。 关于c++-在C++中,在创建对象时,隐式使用new吗?,我们在StackOverflow上找到一个类

c++ - 纯虚函数会阻止隐式生成的 move 构造函数吗?

#includestructtest{virtualvoidfoo()noexcept=0;};structtest2:test{voidfoo()noexceptoverridefinal{}};//failsstatic_assert(std::is_move_constructible::value,"testnotmoveconstructible");//succeedsstatic_assert(std::is_move_constructible::value,"test2notmoveconstructible");(Live)根据cppreference.com(据我

c++ - 为什么隐式转换成员函数重载按返回类型工作,而普通函数不允许这样做?

C++不允许基于方法返回类型的多态性。但是,当重载隐式转换成员函数时,这似乎是可能的。有人知道为什么吗?我认为运算符在内部就像方法一样被处理。编辑:这是一个例子:structfunc{operatorstring(){return"1";}operatorint(){return2;}};intmain(){intx=func();//callsintversionstringy=func();//callsstringversiondoubled=func();//callsintversioncout 最佳答案 转换运算符实际上

c++ - 在赋值运算符函数中,数组是否隐式为 memcpy

好的。我们知道下面的代码无法编译。charsource[1024];chardest[1024];//Fail.Usememcpy(dest,source,sizeof(source));instead.dest=source;但是,下面的代码可以正确编译和运行。classA{chardata[1024];};Asource;Bdest;dest=source;我想知道,在运算符赋值函数中,数组是否会隐式memcpy?以下是完整的测试代码。#include#includeclassA{public:chardata[1024];};intmain(){{Asource;Adest;//