怎么写出优雅的C++代码
本篇内容主要讲解“怎么写出优雅的C++代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么写出优雅的C++代码”吧!
目前累计服务客户上1000+,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供成都网站建设、网站设计、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。成都创新互联始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。
工欲善其事必先利其器,优雅的代码离不开静态代码检查工具,大家可能平时使用较多的是cppcheck,但今天我想跟大家分享另一个静态代码检查工具clang-tidy。
不同于cppcheck使用正则表达式进行静态代码分析,clang-tidy是基于语法分析树的静态代码检查工具,虽然它的速度比正则表达式慢一些,但是它检查的更准确、全面,而且不仅可以做静态检查,还可以做一些修复工作,自行添加一些自定义检查规则。
话不多说,上代码:
#includeint main() { int a = 1.2; return 0; }
这里有隐式类型转换,可以使用clang-tidy来检测:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7748 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:20:13: warning: implicit conversion from 'double' to 'int' changes value from 1.2 to 1 [clang-diagnostic-literal-conversion] int a = 1.2; ^ Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
这里也许你有疑问了,这不就是一个普通的编译警告嘛,正常使用编译器也可以检查出来,那再看一段代码:
#includeint main() { char* d = NULL; return 0; }
我们都知道在C++中应该更多的使用nullptr而不是NULL,这里使用了NULL而不是使用nullptr,可能我们在开发过程中没有注意到这种用法,所以clang-tidy派上了用场:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7748 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:20:15: warning: use nullptr [modernize-use-nullptr] char* d = NULL; ^~~~~ nullptr Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
再举一个例子:
struct Base { virtual void func() { } }; struct Derive : Base { virtual void func() { } };
这里可能我们乍一看没有任何问题,其实在C++11里派生类继承父类,重写了某些函数时最好加上override关键字,通过clang-tidy还是可以检测出来:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7749 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:14:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [hicpp-use-override] virtual void func() { ~~~~~~~~~~~~~^ override Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
该工具还可以检查代码是否符合编码规范,例如Google编码规范等,看这段头文件相关代码:
#include#include #include
这里其实有一点点问题,头文件引用顺序不满足编码规范,这里其实clang-format都可以检测出来,但clang-tidy也可以检测出来,通过-fix还可以进行自动修复:
~/test$ clang-tidy -checks=* test_lint.cpp -- 8961 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:2:1: warning: #includes are not sorted properly [llvm-include-order] #include^ ~~~~~~~~ Suppressed 8960 warnings (8960 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well
它还可以检测隐藏的内存泄漏:
int main() { char* ct = (char*)malloc(323); return 0; }
这是使用clang-tidy的检测结果:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7756 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:20:5: warning: initializing non-owner 'char *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory] char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:20:5: warning: use auto when initializing with a cast to avoid duplicating the type name [hicpp-use-auto] char* ct = (char*)malloc(323); ^~~~~ auto /home/wangzhiqiang/test/test_lint.cpp:20:11: warning: Value stored to 'ct' during its initialization is never read [clang-analyzer-deadcode.DeadStores] char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:20:11: note: Value stored to 'ct' during its initialization is never read /home/wangzhiqiang/test/test_lint.cpp:20:16: warning: C-style casts are discouraged; use static_cast [google-readability-casting] char* ct = (char*)malloc(323); ^~~~~~~~~~~~~ ~ static_cast( ) /home/wangzhiqiang/test/test_lint.cpp:20:16: warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast] /home/wangzhiqiang/test/test_lint.cpp:20:23: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:21:5: warning: Potential leak of memory pointed to by 'ct' [clang-analyzer-unix.Malloc] return 0; ^ /home/wangzhiqiang/test/test_lint.cpp:20:23: note: Memory is allocated char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:21:5: note: Potential leak of memory pointed to by 'ct' return 0; ^ Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well
clang-tidy还有很多高端功能,大概可以检测出250种问题,大体主要分为几大类:
abseil:检测abseil库的相关问题
android:检测Android相关问题
boost:检测boost库的相关问题
cert:检测CERT的代码规范
cpp-core-guidelines:检测是否违反cpp-core-guidelines
google:检测是否违反google编码规范
llvm:检测是否违反llvm编码规范
performance:检测性能相关的问题
readability:检测与可读性相关,但又不属于某些编码规范的问题
modernize:检测是否使用现代C++11相关的代码问题
而且适用于Windows/Linux/MacOS多平台,还支持命令行,CLion/VSCode/VSStudio插件等,检测规则还可以定制,重要的是免费开源,快去用起来吧,写出优雅的C++代码~
到此,相信大家对“怎么写出优雅的C++代码”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
新闻标题:怎么写出优雅的C++代码
网页地址:http://ybzwz.com/article/ggghgd.html