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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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