草庐IT

conversion-operator

全部标签

c++ - 为什么 <iostream> operator<< 选择明显错误的重载?

考虑这段代码:#includeusingnamespacestd;classX{public:operatorconstwchar_t*()const{returnL"Hello";}};voidf(constvoid*){wcout输出为(使用VS2015C++编译器编译):f(constwchar_t*)00118B30所以看起来编译器选择了预期的constwchar_t*f过载(因为存在从X到constwchar_t*的隐式转换)。但是,似乎wcout选择constvoid*过载,而不是constwchar_t*一个(打印地址,而不是wchar_t字符串)。这是为什么?P.S.我

c++ - C++11 中的缩小转换 : what is the "actual value after conversion"?

以下代码在C++11中是否合法?int16_tx{0xaabb};int64_txxxx{0xaaaabbbbccccdddd};代码来自《TheC++ProgrammingLanguage》第4版(第150页)。我们知道,列表初始化是不允许窄化转换的,在标准的窄化转换定义中,我们有:Anarrowingconversionisanimplicitconversion—[...]—fromanintegertypeorunscopedenumerationtypetoanintegertypethatcannotrepresentallthevaluesoftheoriginaltyp

c++ - 不一致的警告 "conversion from ' const unsigned char' to 'const float' requires a narrowing conversion”

VisualC++2017和gcc5.4产生conversionfrom'constunsignedchar'to'constfloat'requiresanarrowingconversion警告LineB但没有此代码段中的A行:#includeintmain(){constunsignedcharp=13;constfloatq=p;//LineAstd::cout这个警告有效吗?为什么LineB的处理方式与LineA不同? 最佳答案 警告有效,来自C++11narrowingconversions在aggregateiniti

c++ - std::thread::id 的 std::operator== 中的段错误

我遇到了一个我不确定如何解决的问题。我认为这是GCC和/或libstdc++中的一个问题。我正在运行带有GCC4.8.2-19ubuntu1、libstdc++3.4.19(我相信?Howdoyoufindwhatversionoflibstdc++libraryisinstalledonyourlinuxmachine?)和boost1.55的Ubuntu14.04LTS。代码如下://http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/tutorial.html//withaslightmodificationtoe

c++ - operator new 的任何实现是否返回指向零大小数组保护页的指针?

相关:C++newint[0]--willitallocatememory?标准在5.3.4/7中说:Whenthevalueoftheexpressioninadirect-new-declaratoriszero,theallocationfunctioniscalledtoallocateanarraywithnoelements....并且,在3.7.3.1/2中:Theeffectofdereferencingapointerreturnedasarequestforzerosizeisundefined....但是,指针不能是空指针。由于实际取消引用指针是未定义的行为,是否

c++ - C 和 C++ : Difference between Casting and Conversion

下面代码中第2行和第3行有什么区别吗?编译器在每种情况下做什么?charch='A';//line1inti=ch;//line2intj=(int)ch;//iine3一般来说,转换和转换(在C和C++中)有什么区别? 最佳答案 最终效果没有区别。cast是使用显式的、通用的、内置的castnotation进行转换。尽管在某些情况下,当我们指的是从Derived*到Base*(或从Derived&到Base&)的隐式转换时,我们会说“向上转换”。在某些情况下,人们定义了新的转换符号。术语的上述定义只是一个操作定义,也就是说,它不是

c++ - 使用operator[]时如何区分读/写操作

我需要编写一个带有重载运算符[]的类,当使用运算符[]读取或写入数据时,它具有不同的行为。举一个我想要实现的实际例子,假设我必须编写一个名为PhoneBook的类的实现,它可以按以下方式使用:PhoneBookphoneBook(999999);//999999isthedefaultnumberwhichshouldbe//usedwhencallingsomeonewhoisnotinthephonebookphoneBook["Paul"]=234657;//addsPaul'snumberphoneBook["John"]=340156;//addsJohn'snumber//

c++ - boost::noncopyable 的 unordered_map 无法从 operator[] 返回引用

为了演示我的问题,请考虑这个无法编译的简单程序:#include#includeclassfoo:boost::noncopyable{};intmain(){std::unordered_mapm;auto&element=m[0];return0;}使用当前版本的boost(1.52),VisualStudio2012返回错误:无法访问类“boost::noncopyable_::noncopyable”中声明的私有(private)成员。std::unordered_map的运算符[]返回对所提供键处元素的引用,乍一看似乎应该有效——我要求的是对元素的引用,而不是它的拷贝.我对这

c++ - 在 C++11 中根据派生类 `operator==` 定义基类 `operator==`?

假设我有一个类型层次结构:structB{...};structD1:B{...};structD2:B{...};...structDn:B{...};每个Di都有自己的operator==定义:structDi:B{booloperator==(constDi&)const{...}...};我现在要定义Boperator==这样:structB{booloperator==(constB&that)const{//psuedo-codeleti,suchthedynamictypeofthisisDiletj,suchthedynamictypeofthatisDjif(i!=j

c++ - "Universal character name conversion"在 C++ 中是什么意思?

C++98显然将此作为编译阶段的标准之一。什么意思,为什么一开始就执行? 最佳答案 通用字符名称类似于\uFFFD或\U0010FFFD。这是一种在源代码中写入字符的方法,其中源代码编码不包含该字符。C++规定在翻译的第一阶段将不在基本源字符集中的字符转换为通用字符名称。这样做的原因是通用字符名称和不在基本源字符集中但在源字符集中的字符得到相同的处理。as-if规则意味着实际上不需要实现来执行此通用字符名称转换,只要它把写为通用字符名称的扩展字符与字面上显示的扩展字符相同在源代码中。 关