Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent certain std functions from being called

Tags:

c++

For certain reasons we should not use certain std functions like std::sort() in our code base (we have our own implementations for those).

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.

like image 414
nimcap Avatar asked Aug 21 '19 09:08

nimcap


2 Answers

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.

I would suggest you to create a custom check for clang-tidy and add a CI job to run it on your codebase. This will take some time but I believe this is the best option.

like image 140
Dmitry Gordon Avatar answered Nov 11 '22 17:11

Dmitry Gordon


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.

like image 3
πάντα ῥεῖ Avatar answered Nov 11 '22 16:11

πάντα ῥεῖ