nimcap
nimcap

Reputation: 10503

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).

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.

Upvotes: 6

Views: 170

Answers (3)

Liviu Stancu
Liviu Stancu

Reputation: 45

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.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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.

Upvotes: 3

Dmitry Gordon
Dmitry Gordon

Reputation: 2324

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.

Upvotes: 5

Related Questions