defined function return type problem

This is a discussion on defined function return type problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I was trying to use #define to build several functions that all contains similar code but with different variables ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    defined function return type problem

    Hi, I was trying to use #define to build several functions that all contains similar code but with different variables types and return types, etc. The following example will look useless, but I found out that it can't work because the compiler removes my whitespaces. I would like to know if it is possible to achieve something like this:


    Code:
    #define MAKE_FUNCTION(FUNC_NAME) bool ##FUNC_NAME##(void) {return 0;}
    
    MAKE_FUNCTION(some_func)	//Expands to "boolsome_func(void) {return 0;}" and gives error
    So my problem is that the compiler deletes the space between the return type and the function's name. However, if the macro would have returned "bool*" for example, it would have compiled correctly because "bool*some_func" gets separeted with the pointer. But... Is it supposed to be possible for my "bool some_func" case?

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    How about
    Code:
    #define MAKE_FUNCTION(FUNC_NAME) bool FUNC_NAME##(void) {return 0;}
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Thanks alot it works just fine! I thought that not writting a "##" before the FUNC_NAME would have caused a compilation error, since the macro params should always be surrounded with "##".

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by mikahell View Post
    ...since the macro params should always be surrounded with "##".
    They should not.
    Perhaps you better read up on the ## preprocessor token.
    Macro params should simply be typed as the name you gave them.
    More specifically, ## allows you to merge a token with another - so if you type the argument name FUNC_NAME and then ##(void) it allows the preprocessor to merge the string into (assume FUNC_NAME is foo) foo(void).
    When you did ##FUNC_NAME, I assume the preprocessor ignored the space and merged bool with the function name, so it became boolfoo.
    Last edited by Elysia; 01-12-2008 at 03:49 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Is this something a template can do better?

  6. #6
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well, I guess that with the little example I have shown, templates would have been a better solution, but in my real application, I can't use a templated function there, unless I would use template specialisations thing.

    Thanks to all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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