On 2014-06-13, ballsystemlord <ballsystemlord@no-mx.forums.opensuse.org> wrote:
> I just created the code quickly, it was never supposed to run.
Ahh. OK.
On 2014-06-13, ballsystemlord <ballsystemlord@no-mx.forums.opensuse.org> wrote:
> I thought that macros were expanded by cpp and so never -actually-
> returned anything.
I’m confused. You were referring to C in your original post (with C code) and now you are referring to CPP. If you
compiling `gcc a.c’, then that’s C and has nothing to do with CPP/C++.
This makes lots of duplicate code be able to be
expressed in a single statement, that would be: TESTME
Undoubtedly, you are about to recommend a function or some other method
of avoiding duplication.
Not necessarily. If you’re really using C++, and want index bounds checking on arrays, then I strongly suggest you use
C++'s containers (e.g. vectors or deques) rather than constructing your own methods. They are going to be more efficient
than anything you write, unless you are happy to code in inline assembler (which I do).
I think that the testme macro, as it is the
real thing, demonstrates that it is to small to be worth putting in a
function (unless the function is to be labeled inline but let’s not get
into such matters,)
This is a perfectly legitimate case for using an inline function so why not use one?
and it is impractical to use less tests as the code
I’m writing is using re2c and that means I’m writing a lexical analyzer
for speed. If you look at the examples you will note that it is
impractical to put the test into the loop.
If you are coding in C++ rather than C, then I strongly suggest you read Agner Fog’s guide of optimising C++.
But back to our original question, what’s the difference from writing
the ternary operator in the macro vs. in the function’s body itself
since the macro gets expanded into the function anyway.
Nothing. But you may want to know the result of the ternary condition for more than just the reason of the assignment of
a single variable. Repeated use of the same macro will mean the generated assembler will evaluate in full (i.e. test the
condition) every time it is called, even if the code logic dictates the same result every time. This is inefficient.
“You could just delete everything from ‘?’ onwards and it will probably
compile fine.”
Your right, and that’s what’s so confusing about the whole thing. You
could substitute the macro in by hand and it still would not compile, so
what is the ternary operator that it can’t accept return.
…because your falseValue of the ternary operation is `return(0)’ which is wrong and should generate an error.
> An why does this work?
>
> Code:
> --------------------
> #define TESTME(pos, maxpos) ((pos) <= (maxpos)) ? : pos++; return(0);
> --------------------
This works because the falseValue of the ternary operation is pos++'. The
return(0)’ has absolutely nothing to do with
the ternary operation (it’s after a `;’ which delimits the end of the ternary operation) and all it will do is make the
function return every time you use the macro. C
After all it’s just 2 commands instead of one following the ternary
operator.
Consider this code, using the macro definition for TESTME you’ve written above:
#include <stdio.h>
#define TESTME(pos, maxpos) ((pos) <= (maxpos)) ? : pos; return(0);
int main(void)
{
int a = 0;
int b = 100;
int isEven = b % 2 ? 0 : 1;
isEven = !(b % 2);
TESTME(a, b);
while(a++)
{
TESTME(a, b);
printf("%d
", a);
}
printf("%d
", a);
return(0);
}
Now when I run the code (save as a.c), this is the result…
sh-4.2$ gcc a.c
sh-4.2$ ./a.out
sh-4.2$
…i.e. nothing. All you’ve done with macro is make the main() function return the first time you call TESTME: this has
nothing to do with the ternary operator.
“Ternary allows the value of any variable to be assigned
depending the truth value of another and potentially otherwise unrelated
variable”
I lost something here, assigned to what and where is the assignment and
what value is being assigned? The variable that follows the resulting
truth or false value?
If you’re lost, you need to start at the beginning and learn the fundamental properties of ternary operators. The topic
is completely independent of the use of macros. Ternary operations are identical to inline if' operations. It would be helpful if you could clear if you're learning C or C++. The reason for the need for the distinction is that standard C (until C99, and even then it's an odd implementation) does _not_ support the
bool’ datatype whereas C++ does. I thought
my `isEven’ example was clear, but it seems you are still lost. If you are learning C++, it could show a clearer
example.