6

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.

7
  • 8
    Don't use using namespace std; Commented Aug 21, 2019 at 9:56
  • 1
    No, there's no way you can prevent that but regular code reviews. Commented Aug 21, 2019 at 9:59
  • 3
    I think the only way is using static analyzers to regularly check your codebase Commented Aug 21, 2019 at 10:02
  • @fdan That's indeed some way, but you need to do some configurable static analyzer. Commented Aug 21, 2019 at 10:03
  • maybe by configuring custom error in your compiler, if that's possible Commented Aug 21, 2019 at 10:05

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.