can it be done?
Ex:
getstring(a) ...
getstring(a, b) ...
getstring(a, b, c) ...
can it be done?
Ex:
getstring(a) ...
getstring(a, b) ...
getstring(a, b, c) ...
By macro I'm assuming you mean something like
#define getline(a) //code here
The first thing that came to mind when I read this was why would you want to? Since you can overload functions and inlining functions achieves the same effect as macros there's no need to even write macros, much less overload them.
As for whether you can or not, give it a try.
-Prelude
My best code is written with the delete key.
From my understanding, you should try avoid macros where possible.......They dont always behave as they should and they have no type safety.
Try const functions
>
can it be done?
Ex:
getstring(a) ...
getstring(a, b) ...
getstring(a, b, c) ...
<
No, you just be using the last one defined.
eg. if you called getstring("whatever");
you compiler would say ", expected" where its expecting a comma between arguements.
>
Since you can overload functions and inlining functions achieves the same effect as macros there's no need to even write macros,
<
there's effiency reasons since functions are called at runtime and macros evaluated during preprocessng.
>
From my understanding, you should try avoid macros where possible.......They dont always behave as they should and they have no type safety.
<
with careful use they can be quite handy,
Last edited by no-one; 01-24-2002 at 01:05 PM.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
>there's effiency reasons since functions are called at runtime
inline functions aren't called at runtime. There's no guarantee that a compiler will inline it but you'd have to have a pretty dumb compiler that wouldn't inline something simple that you'd normally write a macro for.
Macros are not called so there is no stack frame overhead. The compiler substitutes word for word your code within the macro when you use it, then compiles the final product.
Macros can be very useful when used correctly but can be very confusing when used too much.
>
There's no guarantee that a compiler will inline
<
the macro is guaranteed to work the way you'd expect, and the function has type checking.
use functions where functions belong and macros where they are needed. its user preference.
>
it but you'd have to have a pretty dumb compiler that wouldn't inline something simple that you'd normally write a macro for.
<
i don't know i've seen some preety complex macros, that i doubt would be inlined or be used effectively as a function.
>Macros are not called so there is no stack frame overhead. The compiler substitutes word for word your code within the macro when you use it, then compiles the final product.
This is exactly what happens with inline functions, but you get the added benefit of compile time type checking.
>use functions where functions belong and macros where they are needed.
Why?
>i don't know i've seen some preety complex macros, that i doubt would be inlined or be used effectively as a function.
An example? Besides if you're writing long and complex macros that are being frequently substituted in your code then you're just going to produce code bloat, which will negate the efficiency benefit you've gained from eliminating the function call.
macros are not checked as thoroughly as regular functions.
Also, try passing an expression like 'x++' to a macro, then pass it to a function which does the same thing -- unless the macro was written specifically to avoid this, you'll come up, many times, with the wrong answer. A function, even an inline one, cannot modify the parameters unless they're passed by reference -- a macro most certainly can, and if badly written, will, modify this.