草庐IT

implicit-conversion

全部标签

c++ - 模板推导与隐式用户定义转换运算符

我试图实现一个涉及模板的用户定义类型转换的小例子。#include#include#include#include#includetemplateconceptboolUIntegral=requires(){std::is_integral_v&&!std::is_signed_v;};classNumber{public:Number(uint32_tnumber):_number(number){if(number==1){number=0;}for(;number>1;number/=10);if(number==0){throwstd::logic_error("scalem

c++ - 禁止精度损失的整数转换

如何防止这样的代码被编译?#include#include#include#includeintmain(){std::vectorv;v.emplace_back(std::numeric_limits::max());std::coutg++和clang-std=c++14-Wall-Wextra-Werror-pedantic-Wold-style-cast-Wconversion-Wsign-conversion甚至不要警告它。该示例还编译时没有警告std::vector 最佳答案 将-Wsystem-headers添加到命

C++-转换函数模板推导,为什么会这样?

classA{structB{};public:staticvoidtest(A::B){}};structC{templateoperatorT(){returnT();}};intmain(){A::test(C());}此代码适用于clang3.7、gcc5.1和vc++14.2。2个问题,1、为什么template可以推导出类型是A::B?(太聪明了!)据我所知,模板通过返回语句而不是参数来推断类型。但是我发现了一些对N460612.3.26感兴趣的东西Aconversionfunctiontemplateshallnothaveadeducedreturntype(7.1.7

c++ - if(double) 有效的 C++ 吗?

我刚遇到这行代码:if(lineDirection.length2()){...}其中length2返回一个double。让我有点困惑的是0.0等同于0、NULL和/或false。这是C++标准的一部分还是未定义的行为? 最佳答案 这是一个非常标准的行为(bool转换)$4.12/1-"Anrvalueofarithmetic,enumeration,pointer,orpointertomembertypecanbeconvertedtoanrvalueoftypebool.Azerovalue,nullpointervalue,

c++ - 将重载运算符重构为非成员函数是否会破坏任何代码?

考虑一个带有重载加法运算符+=和+的遗留类模板>templateclassX{public:X()=default;/*implicict*/X(Tv):val(v){}X&operator+=(Xconst&rhs){val+=rhs.val;return*this;}Xoperator+(Xconst&rhs)const{returnX(*this)+=rhs;}private:Tval;};在代码审查中,观察到+可以根据+=实现,所以为什么不将其设为非成员(并保证左对称)和正确的论点)?templateclassX{public:X()=default;/*implicit*/X

C++ : Ternary Operator (Conditional Operator) and its Implicit Type Conversion Rules

三元运算符的参数是否有隐式类型转换规则?三元运算符总是需要返回相同的类型。此类型仅由第二个和第三个参数(1st?2nd:3rd)确定,因此两个参数都转换为此类型。这种类型是如何确定的?更具体地说,我测试了一个例子:classpointclass{pointclass();pointclass(inti);//(pointclass)(int)operatorbool()const;//(bool)(pointclass)};我有一个类(pointclass),它支持从int进行隐式转换至pointclass和pointclass的隐式转换至bool.inti;pointclassp;b

c++ - Visual Studio 调试与发布版本 : comparing int and float missmatch

看看这个例子:#includeintmain(){inti=16777217;floatf=16777216.0;floatg=i;if(i==f)printf("eq\n");elseprintf("neq\n");if(g==f)printf("eq\n");elseprintf("neq\n");return0;}在Release模式、gcc或g++(4.9.2)中使用VisualStudio2010C++(VS),具有输出eqeq这对我来说是合理的:在第一次比较期间,i被隐式转换为float,其中尾数中的有效位被截断。因此,i和f都具有相同的位模式,相当于相等性。在第二个if中

c++ - 防止隐式转换但允许列表初始化?

假设我有一个类FunctionWrapper定义如下:structFunctionWrapper{FunctionWrapper(std::functionf);//...plusothermembersirrelevanttothequestion};我想阻止std::function的隐式转换至FunctionWrapper,但允许构建FunctionWrapper使用大括号初始化语法(即使用带有单个参数的列表初始化)。换句话说,我想要这样:voidfoo();voidwrap(FunctionWrapper);wrap(foo);//(1)errorwrap({foo});//(

c++ - 错误 83 错误 C2398 : conversion from 'double' to 'float' requires a narrowing conversion

我找到了很多关于这个错误的帖子,但我可以找到克服它的方法。这是触发错误的代码:voidmain(){floatf{1.3};}为什么在初始化列表中没有像其他变量那样发生转换?例如,这工作顺利:floatf=1.3; 最佳答案 您评论说使用1.3会导致您的编译器出错。这意味着您发现了一个编译器错误。标准很清楚这不是缩小转换,因此应该允许。引用N4140(大致为C++14):8.5.4List-initialization[dcl.init.list]7Anarrowingconversionisanimplicitconversion

c++ - 右值引用 : Why aren't rvalues implicitly moved?

在关于C++右值引用(http://www.artima.com/cppsource/rvalue.html)的Artima文章中有这样的话:这就是为什么在向下传递到基类时必须说move(x)而不是只说x的原因。这是移动语义的一个关键安全特性,旨在防止从某个命名变量意外移动两次。我想不出这样的双招可以执行的情况。你能举个例子吗?换句话说,如果T&&的所有成员都是右值引用而不仅仅是引用,会出现什么问题? 最佳答案 考虑这个场景:voidfoo(std::stringx){}voidbar(std::stringy){}voidtest