简体   繁体   English

防止调用某些std函数

[英]Prevent certain std functions from being called

For certain reasons we should not use certain std functions like std::sort() in our code base (we have our own implementations for those). 出于某些原因,我们不应该在我们的代码库中使用某些std函数,比如std::sort() (我们有自己的实现)。

Is there a way to prevent calls to those functions, preferably by raising an error at compile time? 有没有办法阻止对这些函数的调用,最好是在编译时引发错误?

I looked at overriding std functions but it leads to undefined behavior. 我查看了重写std函数,但它导致了未定义的行为。

You shouldn't try override o change functions in a standard library since in the first case you will have ODR violation and in the second case some of the thirdparties may used in your project may be affected. 您不应该尝试覆盖标准库中的更改函数,因为在第一种情况下您将有ODR违规,在第二种情况下,您的项目中可能使用的某些第三方可能会受到影响。

I would suggest you to create a custom check for clang-tidy and add a CI job to run it on your codebase. 我建议你为clang-tidy创建一个自定义检查,并添加一个CI作业来在你的代码库上运行它。 This will take some time but I believe this is the best option. 这需要一些时间,但我相信这是最好的选择。

There's no way you can mark any of the standard functions as unwanted in your code base. 您无法在代码库中将任何标准函数标记为不需要的函数。

You can do regular code reviews, or use a configurable static analysis tool to check committed code for usage of the unwanted functions though. 您可以定期进行代码审查,或使用可配置的静态分析工具来检查已提交的代码,以便使用不需要的函数。
The latter only makes sense with an established CI process for your software. 后者只对您的软件建立CI过程才有意义。

If you would like to prevent complete headers from being included you could use some header file inclusion static analysis tools (that you could add to your build chain. See Header file inclusion static analysis tools? ). 如果您想阻止包含完整的头文件,可以使用一些头文件包含静态分析工具(您可以添加到构建链中。请参阅头文件包含静态分析工具? )。 In case you are trying to prevent from using only certain functions from the std namespace (of course, without modifying the std headers) then I don't think that's possible. 如果你试图阻止只使用std命名空间中的某些函数(当然,不修改std头文件),那么我认为这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM