jjzjj

static_warning

全部标签

c++ - 为什么要经历 static_cast 的麻烦——将数字转换为 double ?

在我正在处理的代码中遇到了这个问题:doublepart2=static_cast(2)*somthing1*(static_cast(1)+something2)+(static_cast(1)/static_cast(2))*something3+(static_cast(1)/static_cast(2))*pow(something4,3);(something是doubles。)我怀疑有一个很好的理由来解决这个问题static_cast(1)之类的,但似乎我可以少打很多字。我不明白什么?提前致谢。 最佳答案 许多stati

c++ - static 关键字会影响作用域吗?

在C89中,static关键字会影响作用域吗?我的软件负责人告诉我:"Avariablemarkedstaticatthetopofafiledoesn'ttechnicallyhaveglobalscopeanylonger.Staticisascopequalifieraswellasastoragekeyword.Scopeisaconceptthatcoversvisibilityofsymbols,thoughvisibilityisautomaticallycompiledtohavestoragedurationintrinsicallytiedinbyalmostall

c++ - 如果我从不调用这个方法,我可以把 static_assert 放在类方法中吗?

这个版本根本无法编译:structA{voidfoo(){static_assert(0,"Fail");}};这个版本编译没有错误(至少在我的编译器版本中):templatestructB{voidfoo(){static_assert(x,"Fail");}};Bb;只有当我调用b.foo();时,第二个版本才编译失败,所以我想知道如果我从不调用方法,标准是否允许使用第二个版本>富?所有编译器都会以相同的方式运行吗?这不是未定义的行为吗?我想在代码中包含static_assert以在某些模板参数满足某些条件时禁止使用模板类的某些方法。static_assert的用法是否正确?我想在

C++ static const 和初始化(有没有惨败)

我在久违后重返C++,我对众所周知的静态初始化问题的理解有些磕磕绊绊。假设我有一个简单的类Vector2,如下所示(请注意,我知道x和y应该与getter和setter私有(private),为简洁起见,这些只是被省略了):classVector2{public:Vector2(floatx,floaty):x(x),y(y){};floatx,y;}现在,如果我想指定一个静态常量成员来表示x和y设置为1的Vector2,我不确定如何进行——静态常量成员是否会陷入静态初始化问题或让他们const意味着他们还好吗?我正在考虑以下可能性:可能性一://.hclassVector2{publ

c++ - 将#warning 放入#define 的正文中

在应该能够在C和C++文件中编译的header中,在VisualStudio(2010)和MinGW(32位-v3.4.5、64位-v4.5.0)中,我试图最小化大小通过更改每一行(其中有很多)://forsymbolA#ifdef__GNUC__#warningSymbolAisdeprecated.UsepredefinedconstcnAinstead.#else#pragmamessage("SymbolAisdeprecated.UsepredefinedconstcnAinstead.")#endif//SameforB//SameforC//...到//definethi

c++ - 带有返回 const 引用的隐式转换运算符的类的 static_cast<> 行为

我有以下类(class)(精简后只包含相关部分):#includeclassText{private:std::string_text;public:Text(std::string&&text):_text(std::move(text)){}operatorconststd::string&()const{return_text;}};我的问题是:如果我想获得一个conststd::string&,我可以这样做而不会受到任何惩罚吗:Texttext("fred");auto&s=static_cast(text);或者这会构造一个我最终得到引用的中间std::string吗?这种情

c++ - 解决 "only static const integral data members can be initialized within a class"编译错误

以下创建全局对象会导致编译错误。#include"stdafx.h"#includeusingnamespaceSystem;usingnamespacestd;#pragmahdrstopclassTester;voidinput();classTester{staticintnumber=5;public:Tester(){};~Tester(){};voidsetNumber(intnewNumber){number=newNumber;}intgetNumber(){returnnumber;}}TestertesterObject;voidmain(void){cout>ne

c++ - 此函数或变量可能不安全。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS

我正在处理C++DDL,但是我在某些地方遇到了以下问题:C4996'sprintf':Thisfunctionorvariablemaybeunsafe.Considerusingsprintf_sinstead.Todisabledeprecation,use_CRT_SECURE_NO_WARNINGS.Seeonlinehelpfordetails.我确实尝试了#define_CRT_SECURE_NO_WARNINGS,但问题仍然存在。这是代码:sprintf(szDebugString,"%s:0x%x(%s%s%i)",ptrName,(DWORD)funcPtr,inte

c++ - static_cast 可以将非空指针转换为空指针吗?

我需要为回调函数编写代码(它将在ATL中调用,但这并不重要):HRESULTcallback(void*myObjectVoid){if(myObjectVoid==0){returnE_POINTER;}CMyClass*myObject=static_cast(myObjectVoid);returnmyObject->CallMethod();}这里的void*保证是指向CMyClass的指针,所以static_cast是合法的。我关心的是代码必须尽可能可移植(至少对于较新版本的VisualC++)。所以super偏执狂我也倾向于检查CMyClass*指针-我的意思是如果结果为空

c++ - 这个 backward_warning.h #warning 来自哪里?

在不查看我的XCode项目中的每个源文件的情况下,有没有办法找出哪个#include触发了以下警告?#warningThisfileincludesatleastonedeprecatedorantiquatedheader.Pleaseconsiderusingoneofthe32headersfoundinsection17.4.1.2oftheC++standard.ExamplesincludesubstitutingtheheaderfortheheaderforC++includes,orinsteadofthedeprecatedheader.Todisablethisw