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

前端 未结 8 2069
失恋的感觉
失恋的感觉 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:01

    I suspect that the poster was actually looking for something more along the lines of SFINAE checking/dispatch. With C++ templates, can define to template functions, one which calls the desired function (if it exists) and one that does nothing (if the function does not exist). You can then make the first template depend on the desired function, such that the template is ill-formed when the function does not exist. This is valid because in C++ template substitution failure is not an error (SFINAE), so the compiler will just fall back to the second case (which for instance could do nothing).

    See here for an excellent example: Is it possible to write a template to check for a function's existence?

提交回复
热议问题