草庐IT

static-code-analysis

全部标签

c++ - 将 C 风格的转换更改为 static_cast 总是安全的吗?

由于cppcheckcstyleCast样式警告,我正在尝试消除代码库中的所有C样式转换。将C风格的转换更改为static_cast总是安全的吗?安全,我的意思是,是否存在旧的C风格转换可以正常工作,但static_cast会引发错误或未定义行为的情况?type1a;type2b=(type2)a;//Cstylecasttype2b=static_cast(a);//Isthisalwaysavalidreplacementforabovecast? 最佳答案 C风格的转换通常是static_cast的组合或reinterpret

c++ - 如何将 `boost::static_visitor` 实例传递给函数

我正在使用boost::variant在我的项目中经常出现。我的同事们现在想出了传递特定boost::static_visitor实例的想法。以自定义访问类型。她有一些代码如下:#include#includetypedefboost::variantTVar;structVisitor1:publicboost::static_visitor{templateresult_typeoperator()(constT&){return42;}};structVisitor2:publicboost::static_visitor{templateresult_typeoperator(

c++ - C++中static的含义

我认为我的C++相当不错,事实证明我不是。我之前问过一个问题:C++constlvaluereferences其中一个答案中有以下代码:#includeusingnamespacestd;int&GenX(boolreset){staticint*x=newint;*x=100;if(reset){deletex;x=newint;*x=200;}return*x;}classYStore{public:YStore(int&x);int&getX(){returnmy_x;}private:int&my_x;};YStore::YStore(int&x):my_x(x){}intma

c++11:为什么 std::forward 中的 static_assert 是必需的?

在move.h中,forward有两个重载templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&__t)noexcept{returnstatic_cast(__t);}templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&&__t)noexcept{static_assert(!std::is_lvalue_reference::value,"templateargumentsubstituting_Tpisanlvalueref

c++ - 我可以更改 Code::Blocks 自动缩进的行为吗?

我曾经使用MicrosoftVisualStudio2005编写C++代码,其中的自动缩进行为非常好。但是当我尝试使用Code::Blocks时,我意识到其中的自动缩进行为很糟糕。例如困扰我的事情之一:for(inti=0;i对于if和while也是一样的有什么方法可以改变Code::Blocks中自动缩进的行为,使其与MicrosoftVisualStudio自动缩进一样好? 最佳答案 首先,确保启用智能缩进(设置->编辑器->智能缩进)。它在Ubuntu上仍然对我不起作用,但安装codeblocks-contrib包sudoap

c++ - 使用 static_assert 确定特定模板参数是否为特定无类型类模板

我想要一个将参数限制为仅派生自特定模板类的类型的函数。在这种情况下,basic_string(来自STL-docs)。例如,声明了一个wstring:typedefbasic_string,allocator>wstring;基本思路是这样的:templatevoidstrings_only_please(TStringmessage){static_assert(is_base_of::value,"Notastringtype!");}当然,虽然没有指定basic_string,但它无法编译……它需要一个真实的类型。(虽然我可能只对少数实际字符串类型进行硬编码,但我正在寻找针对此模

C++ 模板 : How to conditionally compile different code based on data type?

这里有一个小例子来说明我的问题的本质:#includeusingnamespacestd;typedefcharachar_t;templateclassSTRING{public:T*memory;intsize;intcapacity;public:STRING(){size=0;capacity=128;memory=(T*)malloc(capacity*sizeof(T));}constSTRING&operator=(T*buf){if(typeid(T)==typeid(char))strcpy(memory,buf);elsewcscpy(memory,buf);ret

JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is

JSONparseerror:Illegalcharacter((CTRL-CHAR,code31)):onlyregularwhitespace(\r,\n,\t)isallowedbetweentokens;nestedexceptioniscom.fasterxml.jackson.core.JsonParseException:Illegalcharacter((CTRL-CHAR,code31)):onlyregularwhitespace(\r,\n,\t)isallowedbetweentokensat[Source:(org.springframework.util.Strea

c++ - Windows XP 样式 : Why do we get dark grey background on static text widgets?

我们正在使用C++和Win32编写Windows桌面应用程序。我们的对话框具有“WindowsXP风格”的丑陋外观:静态文本的背景是灰色的。对话框背景也是灰色的,这不是问题,但是在选项卡控件中,背景是白色的,文本的灰色背景非常明显。过去我们自己绘制了很多控件,但现在我们正在尝试尽可能多地使用标准外观,并尽可能避免覆盖标准行为。我们使用的是Win32API,它有点过时了,但我认为即使使用ATL也会出现问题。我们正在创建一个DIALOGTEMPLATE。文本位于“静态”控件(0x0082)中。我们为样式设置的唯一标志是“SS_LEFT”。文本控件位于选项卡控件内:“SysTabContro

c++ - 如何关闭 Code::Blocks 的自动完成

当我在Code::Blocks中输入代码时,我更愿意直接输入代码,而无需让编辑器插入右括号和方括号。我在“设置”>“编辑器”>“代码完成”中选择了禁用代码完成,但此功能不会禁用带有结束字符的自动完成功能。在Code::Blocks中关闭此功能的正确设置是什么?(请注意代词审查员可能会发生这种情况:https://www.theregister.co.uk/2019/10/08/stack_overflow_apology/) 最佳答案 “设置->编辑器...->常规设置->缩进选项->大括号补全”