草庐IT

c++ - 隐式转换和运算符重载

我有以下代码:structhelper{templatehelper(Tconst&);};helperoperator*(helperconst&);structA{};intmain(){//(1)Aa;sizeof(*a);//(2)inti;sizeof(*i);}案例(1)编译良好,我知道它正在使用隐式转换为helper类型和给定的运算符重载。然而,对于情况(2),我得到一个编译器错误:invalidtypeargumentofunary'*'(have'int')为什么隐式转换用于A类型而不是int? 最佳答案 当不涉及

c++ - 显式关键字应用于运算符而不是构造函数

在下面的类中,为什么要使运算符显式。我认为explicit是为了防止构造函数的隐式调用?classContent{public:virtual~Content()=0;virtualexplicitoperatorfloat&();virtualexplicitoperatorlonglong&();virtualexplicitoperatorstd::string&()} 最佳答案 Ithoughtthatexplicitwastopreventimplicitcallingofconstructors?自C++11起它也适用于

响应式导航

我正在尝试使我的导航响应迅速,我不知道该如何,它想要它,以免它跳到下一行,我知道填充物正在引起这一点,我可以通过使用百分比作为宽度,但是看起来很丑陋。这就是页面在正常分辨率上的外观:http://imgur.com/a/yo7xh这就是我放大时的外观..(这就是我不想发生的):http://imgur.com/a/a3siu我该怎么做才能防止这种情况发生?导航代码:.header-side.game{float:right;}.header-side.selected{color:#f7c22c;}.header-side.other{float:left;}.bottom-header{he

c++ - 没有将 bool 隐式转换为浮点类型的警告?

看起来这个片段在clang中编译时没有警告,即使使用了-Weverything:doublex;...if(fabs(x>1.0)){...}我错过了什么吗?还是编译器和C++标准认为将bool转换为double是有意义的? 最佳答案 这是使bool成为整数类型的结果。根据C++标准,第3.9.1.6节Valuesoftypeboolareeithertrueorfalse(Note:Therearenosigned,unsigned,short,orlongbooltypesorvalues.—endnote)Valuesofty

c++ - 有没有办法强制用户显式指定模板参数类型?

简短版本:我有一个模板函数,它是“通用的”,但我想强制用户明确指定参数的类型,它们作为参数传递给这个函数。有什么想法吗?长版:听起来像是一个糟糕的设计,但这是我的情况,目前我想不出更好的东西。我试图在一个小的socket类中“实现”::setsockopt(我不想有大量的函数,采用不同的参数和做同样的事情)。例如:templateboolset_option(intlevel,intoption_name,constOPTION_VALUE_TYPE&value){return-1!=::setsockopt(fd_,level,option_name,&value,sizeof(va

c++ - 如何将整数隐式转换为 double ?

inta{5},b{2},c{9};doubled=(double)a/(double)b+(double)c;或者我可以使用static_cast。无论哪种方式都是冗长的,尤其是当公式很长时。有更好的解决方案吗? 最佳答案 你可以乘以1.0:inta{5},b{2},c{9};doubled=1.0*a/b+1.0*c;当你处理总和时,你可以加到0.0:doubled=0.0+a-b+c;大多数编译器会执行优化,以便不评估无意义的操作。仅完成类型转换。请记住,您只需转换每个除法/乘法组中的第一个成员。以任何看似合理的方式这样做。简

c++ - 在两个连续的 pragma omp for 的情况下隐式屏障 vs nowait

查看文档here,以下结构定义明确:#pragmaompparallel//Line1{#pragmaompfornowait//Line3for(i=0;i自从Herethenowaitclauseimpliesthatthreadscanstartonthesecondloopwhileotherthreadsarestillworkingonthefirst.Sincethetwoloopsusethesameschedulehere,aniterationthatusesa[i]canindeedrelyonitthatthatvaluehasbeencomputed.我很难理

c++ - 隐式与显式删除的复制构造函数

我最近在我的C++11代码中遇到了一些奇怪的行为。我有一个类,它只能移动:classonly_move{public:only_move():value(0){}only_move(intvalue):value(value){}only_move(constonly_move&)=delete;only_move&operator=(constonly_move&)=delete;only_move(only_move&&)=default;only_move&operator=(only_move&&)=default;intvalue;};我还有另一个类,其中包含一个only_m

c++ - 为什么 bool 到 string 的隐式转换不是错误?

我仔细查看并尝试在SO上找到类似的问题,但没有找到任何有用的信息。所以,在这里发布我的问题。考虑这个程序:#includevoidfoo(conststd::string&){}intmain(){foo(false);}[Warning]converting'false'topointertypeforargument1of'std::basic_string::basic_string(const_CharT*,const_Alloc&)[with_CharT=char;_Traits=std::char_traits;_Alloc=std::allocator]'[-Wconve

c++ - 在 C++ 中使用隐式转换进行复制初始化

classFoo{public:Foo(floatb){}};classBar{public:Bar(Foofoo){}};intmain(intargc,char*argv[]){Barb1(3.0f);//accept,oneimplicitconvertionhappensthere.Barb2=3.0f;//error:noviableconversionfrom'float'to'Bar'return0;}为什么第二个表达式编译失败?我预计它会调用与第一个表达式相同的转换构造函数。 最佳答案 来自[dcl.init]:Ot