How to Check if the function exists in C/C++

前端 未结 8 2073
失恋的感觉
失恋的感觉 2020-12-01 06:24

Certain situations in my code, i end up invoking the function only if that function is defined, or else i should not. How can i achieve this ?

like:
if (func         


        
8条回答
  •  盖世英雄少女心
    2020-12-01 07:06

    When you declare 'sum' you could declare it like:

    #define SUM_EXISTS
    int sum(std::vector& 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.

提交回复
热议问题