Functions to replace the preprocessor with #defines

This is a discussion on Functions to replace the preprocessor with #defines within the C++ Programming forums, part of the General Programming Boards category; Hello everybody!!! I need help... I think this could have a simple solution, but i'm not getting ... I have ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Functions to replace the preprocessor with #defines

    Hello everybody!!!
    I need help... I think this could have a simple solution, but i'm not getting ...
    I have this following code:

    Code:
    #define PRINT_ME
    
    //(...)
    
    #ifdef PRINT_ME
    #define PRINT1(s1,s2) printf(s1,s2)
    #define PRINT2(s1,s2,s3) printf(s1,s2,s3)
    #else
    #define PRINT1(s1,s2)
    #define PRINT2(s1,s2,s3)
    #endif
    this works fine when i need to use like this

    Code:
    void test()
    {
    	PRINT1("Hello number: %d", 1);
    	PRINT2("Hello both: %d and %d", 1, 2);
    }
    But, now I need to forget the '#defined' and use a global variable to control the PRINT_ME state (because during the execution of my application, i need to change the state of the #define...therefore i can't use #define anymore...i need to use a global variable). And change the PRINT1, and PRINT2 to functions.
    Something like this:

    Code:
    bool print_me = false;
    
    void setPrint(bool status)
    {
    	print_me = status;
    }
    
    bool getPrint()
    {
    	return print_me;
    }
    
    
    template < typename T >
    void PRINT1(char *s1, T s2)
    {
    	printf(s1,s2);
    }
    
    template <class T, class U>
    void PRINT2(char *s1, T s2, U s3)
    {
    	printf(s1,s2,s3);
    }
    
    
    void test()
    {
    	if(getPrint() == true)
    	{
    		PRINT2("number: %d and %d", 1, 2); //don't work:-(
    	}
    }
    I was trying to use templates...because the "printf" function allows any type of variable (%d, %s, %x, %f, etc)..

    Any ideias, or a tutorial that can help me accomplish this?

    Thanks :-)

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    What possible value is there in those defines? Instead of typing PRINT1, you could just type printf, with exactly the same result. I mean, you don't even save on keystrokes to do it.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Are you using C or C++?
    Code:
    template <class T, class U>
    This is clearly not C.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Quote Originally Posted by KBriggs View Post
    What possible value is there in those defines? Instead of typing PRINT1, you could just type printf, with exactly the same result. I mean, you don't even save on keystrokes to do it.
    I'm sorry. This is C++.
    I really need to use this defines as i described..i can't just use printf. I need a global variable that control if I want printf's or not. As before, I use to have those defines, but now I need a global variable to control.
    But I don't know how can I use a function that accept this arguments:
    ("number = %d", 1)...

    thanks

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Moved to C++ board

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Quor
    i can't just use printf. I need a global variable that control if I want printf's or not.
    In other words, your example is wrong. You don't want to do this:
    Code:
    void test()
    {
        if(getPrint() == true)
        {
            PRINT2("number: %d and %d", 1, 2); //don't work:-(
        }
    }
    Rather, you want to do this:
    Code:
    void test()
    {
        print("number: %d and %d", 1, 2);
    }
    Then depending on the value of a global boolean flag, this may or may not print anything at all. Is that correct?

    A solution here is to use a variable argument list. Templates will not offer much since you are going to use printf anyway, which uses a variable argument list. (But see Boost.Format)

    That said, why do you want to do this? If this printing is for debugging, then it is probably fine. But if it is actually part of your program logic, then this is bad: you're using a global variable that can potentially confuse a maintainer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    You can't use #define's or templates in the way you want.

    The reason is that both expansion of macro and template instantiation have finished when the compiler is finished with your code.

    When the program is run, it is too late to change the results of the compilation process.

    If you want the macro to have behaviour that depends on the value of a variable, the macro itself needs to test that variable at run time. For example;
    Code:
    #define PRINT1(s1, s2) do {if (print_me) printf(s1, s2);} while (0)
    I'll leave it as an exercise to work out why I wrapped the logic in do {} while (0)
    Last edited by grumpy; 11-03-2010 at 03:47 AM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  2. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 11:10 PM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  4. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 02:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21