Is an inline function really the same as a macro?

This is a discussion on Is an inline function really the same as a macro? within the C Programming forums, part of the General Programming Boards category; I'm wondering if an inline function is really the same as a preprocessor macro - just with some advantages like ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    13

    Is an inline function really the same as a macro?

    I'm wondering if an inline function is really the same as a preprocessor macro - just with some advantages like type checking et cetera.

    Would that mean if I define
    Code:
    inline void double_it(int a) {
        a += a;
    }
    and I use it like this

    Code:
    int a = 5;
    double_it(a);
    that my var 'a' would be actually 10 afterwards? Or would I still need to use pointers?

    This question just came up when I read through some C tutorial - and I'm almost sure that this isn't working

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    a macro would probably be faster.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    nope, a value will remain 5, why not write a small program to test it?

    I guess that the compiler will generate code for copying "a" to another variable, and then, the code resulted from expanding the inline function will work on a copy of "a."

  4. #4
    Board Conservative UnregdRegd's Avatar
    Join Date
    Jul 2003
    Posts
    154
    An inline function is a feature of C++, not C. The compiler can choose not to inline a function you've declared as inline if it wants. However, the compiler won't turn a macro into a genuine function because it's just preprocessor stuff.
    I am a programmer. My first duty is to God, then to nation, then to employer, then to family, then to friends, then to computer, and finally to myself. I code with dignity, honor, and integrity.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Is an inline function really the same as a macro?

    Originally posted by Sloede
    I'm wondering if an inline function is really the same as a preprocessor macro - just with some advantages like type checking et cetera.
    No. They are very very different. When working with inlined functions, think of it just like it's a regular function in terms of how the data is manipulated, with the desired effect of having the code inserted at the desired location rather than issuing a function call to do the job (although simply using inline doesn't guarantee this result).

    A macro, on the otherhand, is expanded before the program is even compiled and the parameters work as though they are textually inserted into the newly created code. Your example, for instance, does this. Also, macros don't obey scope rules.

    Other differences are that you can take the address of inline functions, whereas you can't take the address of macros. Also, with macros, you can use parameters that you can't easily do with inline functions such as datatype names, expressions, etc. Also, because a macro can be looked at as a textual insert, you can use the results of macros in places where you can't use inline functions. Macro results can be compile-time constants whereas inline function return values can not.

    Example:
    Code:
    #include <stddef.h>
    
    #define STRING_SIZE( num_characters ) (num_characters) + 1
    inline const size_t string_size( size_t num_characters )
    {
      return num_characters + 1;
    }
    
    char String[STRING_SIZE(255)]; // compiles
    //char String[string_size(255)]; // won't compile
    In C++ this "problem" of not being able to use the result where a constant is needed without using a macro can be solved by using template classes with nested constants. This solution takes the benefits of type-safety that inline functions provide, but still allows the result to be used in a constant expression. Macros, be gone!

    C++ example:
    Code:
    #include <cstddef>
    
    template< std::size_t num_characters >
    class string_size
    {
    public:
      static const std::size_t result = num_characters + 1;
    };
    
    char String[string_size< 255 >::result]; // works
    Of course, that's a C++ example so in C that's not an option.

    Some other properties of the template example are that no matter how many times you use string_size< 255 >, the calculation num_characters + 1 will only occur one time, unlike with the macro solution. In this situation the calculation is simple so it won't make much of a difference, but in other cases it can be a fairly complex calculation. Also, since the 255 is a template parameter, it must be a compile-time constant, so you can not use it if the value 255 is stored in a non-const variable (or a const variable initialized via a non-const variable). Lastly, if your calculation requires floating point constants instead of integral types, you can't use the template solution because floating point types can not be used as template values.

    In the end, if you are using C, then there are many cases where you may have to use macros simply because there is no more elegant solution available. In C++, there are still some times where you must use macros (IE to get functionality like assert, etc), but with templating, those situations are considerably less.

    Sorry to go off on a tangent if it was more information than you wanted.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by UnregdRegd
    An inline function is a feature of C++, not C.
    Actually, as of C99, C does allow inline functions as a part of the standard.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    You're kidding me! That answer was bloody awesome! Thank you very much!

    I have to implement an algorithm in C and it uses some really basic functions in each step (like a one-line expression). But the use of macros somehow displeases me as it is like the "dirty" solution. So I thought about using inline-functions (as part of the standard of course) when my initial question arose.

    Again, thanks to all for your support.

    Sloede

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, 09:42 PM
  2. inserting images into file creation inline function
    By WaterNut in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2007, 07:13 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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