How to check if a function exists in C/C++?

When you declare ‘sum’ you could declare it like:

#define SUM_EXISTS
int sum(std::vector<int>& addMeUp) {
    ...
}

Then when you come to use it you could go:

#ifdef SUM_EXISTS
int result = sum(x);
...
#endif

I’m guessing you’re coming from a scripting language where things are all done at runtime. The main thing to remember with C++ is the two phases:

  • Compile time
    • Preprocessor runs
    • template code is turned into real source code
    • source code is turned in machine code
  • runtime
    • the machine code is run

So all the #define and things like that happen at compile time.

….

If you really wanted to do it all at runtime .. you might be interested in using some of the component architecture products out there.

Or maybe a plugin kind of architecture is what you’re after.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)