Thread: macros: can i test if a function was defined?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    macros: can i test if a function was defined?

    can i test if a function was defined for i see what function to use?
    if i call a not defined function, i will get 2 compilers errors. so for resolve the problem, i must use macros.
    i'm trying do some code but without success
    Code:
    #ifdef clsPointer->MouseClick            
                     clsPointer->MouseClick();
            #else
                MouseClick();
            #endif
    these code isn't completed, but for show what i mean.
    if i define the clsPointer->MouseClick, the compiler must use the clsPointer->MouseClick, but if sin't, the compiler must use MouseClick.
    so what i miss on these code?
    Last edited by joaquim; 04-29-2018 at 09:01 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    No, you can't. Not in the way you want, anyway.

    You could have an array of function pointers that its element is NULL when a function doesn't exist, like a C++ map. But of course there's no way to generate it automagically, it'd have to be maintained manually by the user.

    For example:
    Code:
    void foo()
    {
        // ....
    }
    
    void bar()
    {
        // ....
    }
    
    typedef void (*funcPtr)();
    
    std::map<std::string, funcPtr> myFunctions;
    myFunctions["foo"] = foo;
    myFunctions["bar"] = bar;
    
    if (myFunctions.find("bar") != myFunctions.end()) {
        myFunctions["bar"]();
    }
    EDIT: Wait... now that I think about it, aren't virtual methods saved as functions pointers inside an object? If you could check whether a virtual method existed or not, that would be great, but I don't know.
    Last edited by GReaper; 04-29-2018 at 09:53 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, you can't do this with macros, because macros are evaluated before function definitions are evaluated.

    There are several facilities in c++ that allow choosing between a specialized behavior, and a general purpose behavior if the specialization does not exist. These work such that at the call site calls the most general function, but a specialized implementation is invoked based on the type of th object calling it.

    For example
    Code:
    class DefaultContext {};
    struct ClsContext { //I'm only using a struct here to keep the example short. 
        Cls * clsPtr;
    };
    
    void mouseClick(DefaultContext)
    {
       mouseClick();
    }
    
    void mouseClick(ClsContext context)
    {
       context.clsPrt->mouseClick();
    }
    
    int main()
    {
      auto context = someFactoryToGetContext();
      mouseClick(context); // calls the correct function depending on the context type.
    }
    Last edited by King Mir; 04-29-2018 at 11:00 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    i'm sorry, never get notification(i use hotmail) and the options are on
    i have 1 code that tell me if the function is on class, but if it's just prototype, i will get the same
    i can share the code:
    Code:
    template <class Type>class TypeHasToString
    {
        // This type won't compile if the second template parameter isn't of type T,
        // so I can put a function pointer type in the first parameter and the function
        // itself in the second thus checking that the function has a specific signature.
        template <typename T, T> struct TypeCheck;
    
    
        typedef char Yes;
        typedef long No;
    
    
        // A helper struct to hold the declaration of the function pointer.
        // Change it if the function signature changes.
        template <typename T> struct ToString
        {
            typedef void (T::*fptr)();
        };
    
    
        template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::MouseClick >*);//can i change these for receive a function name?
        template <typename T> static No  HasToString(...);
    
    
    public:
        static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));
    };
    i must convert it to a macro(for choose another function name), but the value must be called.
    if the function have a prototype and not defined, i will get '1'.
    if the function isn't on class(prototype and definition), i will get zero.
    from here, can anyone advice me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  2. Using switch statements in function-like macros
    By robot_chicken in forum C Programming
    Replies: 3
    Last Post: 07-25-2009, 03:09 PM
  3. function definition with macros
    By toshog in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2009, 09:22 PM
  4. using functions and macros defined in include/linux/list.h
    By smoking81 in forum Linux Programming
    Replies: 2
    Last Post: 09-27-2008, 04:32 AM
  5. Replies: 14
    Last Post: 03-02-2008, 01:27 PM

Tags for this Thread