473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

#define can't be inside function?

"#define" can only be inside the global scope before main() function.
"#if" can be tested after "#define" is executed. The problem is that
"#define" can't be inside main() function. I do not wish to create multiple
functions which they look almost identical in C++ source code. The C++
compiler should be able to compile one Test() function into two almost
identical functions before they are translated into the machine language
object. The machine language object has two Test() functions, but they
exclude some line parts. First Test() function uses printf("1\n") and
second Test() function uses printf("2\n").
It is the same example that you use MACRO to test the conditional
defines before some lines can be inserted or removed.
If there is no way to test it at compile time, I would have to manual
pasting one Test() function into multiple Test() functions in C++ source
code. Please advise.

Bryan Parkoff

#include <stdio.h>

#define A 1

void Test(void)
{
#if (A == 1)
printf("1\n");
#endif

#if (A == 2)
printf("2\n");
#endif
}

int main(void)
{
Test();
#undef A
#define A 2
Test();

return 0;
}
Jul 23 '05 #1
18 8880

"Bryan Parkoff" <no****@nospam.com> skrev i en meddelelse
news:qd*************@newssvr11.news.prodigy.com...
"#define" can only be inside the global scope before main() function.
This is wrong. #defines can be placed everywhere - it just has to be on a
line by itself (with "#" being the first character of that line).
"#if" can be tested after "#define" is executed. The problem is that
"#define" can't be inside main() function. I do not wish to create
multiple functions which they look almost identical in C++ source code.
The C++ compiler should be able to compile one Test() function into two
almost identical functions before they are translated into the machine
language object. The machine language object has two Test() functions,
but they exclude some line parts. First Test() function uses
printf("1\n") and second Test() function uses printf("2\n").
It is the same example that you use MACRO to test the conditional
defines before some lines can be inserted or removed.
If there is no way to test it at compile time, I would have to manual
pasting one Test() function into multiple Test() functions in C++ source
code. Please advise.

Bryan Parkoff

[snip]

/Peter
Jul 23 '05 #2
Bryan Parkoff <no****@nospam.com> wrote:
| "#define" can only be inside the global scope before main() function.
| "#if" can be tested after "#define" is executed. The problem is that
| "#define" can't be inside main() function. I do not wish to create multiple
| functions which they look almost identical in C++ source code. The C++
| compiler should be able to compile one Test() function into two almost
| identical functions before they are translated into the machine language
| object. The machine language object has two Test() functions, but they
| exclude some line parts. First Test() function uses printf("1\n") and
| second Test() function uses printf("2\n").

That's what templates are for:
template <int a>
void foo()
{
if (a == 1)
printf("1\n");
if (a == 2)
printf("2\n");
}

int main()
{
foo<1>();
foo<2>();
}

| It is the same example that you use MACRO to test the conditional
| defines before some lines can be inserted or removed.

You could of course use some form of macro magic, but templates are
usually prettier.

| If there is no way to test it at compile time, I would have to manual
| pasting one Test() function into multiple Test() functions in C++ source
| code. Please advise.

Compilers today do such optimizations. My g++ do. Do you want to test
things at compile time or before compile time?

--
Robert Bauck Hamar
Jul 23 '05 #3
Robert Bauck Hamar,

I want to say thank you very much for the information. I try to add
comment so you can understand better. Please look at my example. It has
multiple functions. They are almost identical. Sometimes, I use B++, but
sometimes, I use C++. I do not wish to use Test1(), Test2(), and Test3(),
but I use one function as Test(). I expect that Test1(), Test2(), and
Test3() stop to exist in the source code. Please read second paragraph
below my example.

void Test(void)
{
A++;
B++;
C++;
}

void Test1(void)
{
A++;
}

void Test2(void)
{
A++;
B++;
}

void Test3(void)
{
A++;
C++;
}

If Test() contains "if (A == XX)", it should never exist in the machine
language object in both debug version and release version. I expect C++
compiler to remove "if (A == XX)" before A++, B++, and/or C++ can be stored
in the machine language object. Look at my example below. Please read
third paragraph below my example.

int main(void)
{
A = 1;
Test(); // Put A++ only (B++ and C++ are excluded) without "if (A ==
XX)"
A = 2;
Test(); // Put A++ and B++ only (C++ is excluded) without "if (A == XX)"
A = 3;
Test(); // Put A++ and C++ only (B++ is excluded) without "if (A == XX)"
A = 0;
Test(); // Put A++, B++, and C++ without "if (A == XX)"
}

void Test(void)
{
if (A == 1)
A++;
if (A == 2)
B++;
if (A == 3)
C++;
}

After my C++ source code is compiled into machine language object in
both debug version and release version, "if (A == XX)" is removed
automatically, A++, B++, and/or C++ can be preserved or removed. It would
reduce x86 instructions and improve performance.
I know that "if (A == XX)" do not work so it needs one or more variables
like "if (B == XX)" and "if (C == XX)" so they can test correctly.
Do you expect that template is only the answer?

Bryan Parkoff

<ro**********@ifi.uio.no> wrote in message
news:d7**********@readme.uio.no...
Bryan Parkoff <no****@nospam.com> wrote:
| "#define" can only be inside the global scope before main()
function.
| "#if" can be tested after "#define" is executed. The problem is that
| "#define" can't be inside main() function. I do not wish to create
multiple
| functions which they look almost identical in C++ source code. The C++
| compiler should be able to compile one Test() function into two almost
| identical functions before they are translated into the machine language
| object. The machine language object has two Test() functions, but they
| exclude some line parts. First Test() function uses printf("1\n") and
| second Test() function uses printf("2\n").

That's what templates are for:
template <int a>
void foo()
{
if (a == 1)
printf("1\n");
if (a == 2)
printf("2\n");
}

int main()
{
foo<1>();
foo<2>();
}

| It is the same example that you use MACRO to test the conditional
| defines before some lines can be inserted or removed.

You could of course use some form of macro magic, but templates are
usually prettier.

| If there is no way to test it at compile time, I would have to
manual
| pasting one Test() function into multiple Test() functions in C++ source
| code. Please advise.

Compilers today do such optimizations. My g++ do. Do you want to test
things at compile time or before compile time?

--
Robert Bauck Hamar

Jul 23 '05 #4
Robert Bauck Hamar,

I want to let you know that your example code does not work. If
template function reads "1" and "2", then it will execute "2" twice. It
never shows "1" before "2". Look at my example that is similiar yours.

#include <stdio.h>

template <int A>
void Test(void)
{
if (A == 1)
printf("1\n");
if (A == 2)
printf("2\n");
if (A == 3)
printf("3\n");
printf("\n");
}

int main(void)
{
Test<1>();
Test<2>();
Test<3>();

return 0;
}

Output shows "3" in first line, second line, and third line. Is there a
fix?

Bryan Parkoff
<ro**********@ifi.uio.no> wrote in message
news:d7**********@readme.uio.no...
Bryan Parkoff <no****@nospam.com> wrote:
| "#define" can only be inside the global scope before main()
function.
| "#if" can be tested after "#define" is executed. The problem is that
| "#define" can't be inside main() function. I do not wish to create
multiple
| functions which they look almost identical in C++ source code. The C++
| compiler should be able to compile one Test() function into two almost
| identical functions before they are translated into the machine language
| object. The machine language object has two Test() functions, but they
| exclude some line parts. First Test() function uses printf("1\n") and
| second Test() function uses printf("2\n").

That's what templates are for:
template <int a>
void foo()
{
if (a == 1)
printf("1\n");
if (a == 2)
printf("2\n");
}

int main()
{
foo<1>();
foo<2>();
}

| It is the same example that you use MACRO to test the conditional
| defines before some lines can be inserted or removed.

You could of course use some form of macro magic, but templates are
usually prettier.

| If there is no way to test it at compile time, I would have to
manual
| pasting one Test() function into multiple Test() functions in C++ source
| code. Please advise.

Compilers today do such optimizations. My g++ do. Do you want to test
things at compile time or before compile time?

--
Robert Bauck Hamar

Jul 23 '05 #5
In article <qd*************@newssvr11.news.prodigy.com>,
no****@nospam.com says...
"#define" can only be inside the global scope before main() function.
"#if" can be tested after "#define" is executed. The problem is that
"#define" can't be inside main() function.
What makes you think that? I'm not sure it does much (if anything) to
help the problem you cite below, but it's entirely possible to do a
#define inside of main (or another function of your choice).
I do not wish to create multiple
functions which they look almost identical in C++ source code.
Indeed -- one of the reasons for using functions is to avoid code
duplication.
#include <stdio.h>

#define A 1

void Test(void)
{
#if (A == 1)
printf("1\n");
#endif

#if (A == 2)
printf("2\n");
#endif
}

int main(void)
{
Test();
#undef A
#define A 2
Test();

return 0;
}


There are quite a few ways of handling this. The first (and most
obvious) would be to simply pass A as a parameter:

#define A 1

void Test(int A) {
printf("%d\n", A);
}

int main() {
Test(A); // prints 1

#undef A
#define A 2

Test(A); // prints 2

return 0;
}

If, for some reason, it's necessary that you print a string literal
instead of converting A from an int at run-time, you can do that with
the preprocessor:

#include <stdio.h>

#define make_string(a) #a
#define str(x) make_string(x)

#define Test(A) printf(str(A) "\n")

int main() {
#define A 1
Test(A);
#undef A
#define A 2
Test(A);
return 0;
}

The make_string(x)/str(x) is a fairly well-known pre-processor idiom.
The '#' is the pre-processor's stringizing operator, which produces a
quoted string literal of whatever it's applied to. The extra level of
macro expansion makes the preprocessor expand the parameter out to
its value instead of its name. Once we have the value in a string, we
put it next to the "\n" string, so the compiler will concatenate the
two into a single string for us.

If you really can't pass A as a parameter, you can do something like
this:

#include <stdio.h>

#define make_string(a) #a
#define str(x) make_string(x)

#define Test() printf(str(A) "\n")

int main() {
#define A 1
Test();
#undef A
#define A 2
Test();
return 0;
}

Personally, I'd advise against this though -- it usually loses far
more than it gains.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 23 '05 #6
Jerry,

Your previous example below does not work. It only prints "1" in first
line and second line. I can assume that "#undef A" and "#define A 2" are
not working.
#define A 1

void Test(int A) {
printf("%d\n", A);
}

int main() {
Test(A); // prints 1

#undef A
#define A 2

Test(A); // prints 2

return 0;
}


Bryan Parkoff
Jul 23 '05 #7
In article <yI*************@newssvr30.news.prodigy.com>,
no****@nospam.com says...
Jerry,

Your previous example below does not work. It only prints "1" in first
line and second line. I can assume that "#undef A" and "#define A 2" are
not working.


I think I must have accidentally copied and pasted the wrong version
-- as it stands right now, it doesn't even compile -- this:

#define A 1

void Test(int A) {
// ...

expands to:

void Test(int 1) {

which isn't allowed. Fixing that gives:

#include <stdio.h>

#define A 1

void Test(int a) {
printf("%d\n", a);
}

int main() {
Test(A); // prints 1

#undef A
#define A 2

Test(A); // prints 2

return 0;
}

which prints out:

1
2

as it should. If you're getting 1 twice as you describe above, then
I'd have to guess it's a problem with (the version of) whatever
compiler you're using.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 23 '05 #8
Jerry,

Yes, it still gets "1" twice. I use Microsoft Visual C++ 6.0. Do you
know if I should be able to fix this problem by using C++ Compiler's option?
Or...do you suggest me to use another C++ Compiler?

Bryan Parkoff

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.sunsite.dk...
In article <yI*************@newssvr30.news.prodigy.com>,
no****@nospam.com says...
Jerry,

Your previous example below does not work. It only prints "1" in
first
line and second line. I can assume that "#undef A" and "#define A 2" are
not working.


I think I must have accidentally copied and pasted the wrong version
-- as it stands right now, it doesn't even compile -- this:

#define A 1

void Test(int A) {
// ...

expands to:

void Test(int 1) {

which isn't allowed. Fixing that gives:

#include <stdio.h>

#define A 1

void Test(int a) {
printf("%d\n", a);
}

int main() {
Test(A); // prints 1

#undef A
#define A 2

Test(A); // prints 2

return 0;
}

which prints out:

1
2

as it should. If you're getting 1 twice as you describe above, then
I'd have to guess it's a problem with (the version of) whatever
compiler you're using.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #9
In article <Z7**************@newssvr30.news.prodigy.com>,
no****@nospam.com says...
Jerry,

Yes, it still gets "1" twice. I use Microsoft Visual C++ 6.0. Do you
know if I should be able to fix this problem by using C++ Compiler's option?
Or...do you suggest me to use another C++ Compiler?


This seems very strange -- I originally tested it with VC++ 7.1, but
doing a quick check with 6.0, the result is identical. Just for fun,
I also tested it with Borland 5.5 and Comeau 4.3.4, all with
identical results.

Just for fun, let's try a little exercise. I've rewritten the code to
eliminate the header, and just declare printf directly -- generally a
poor idea, but helpful for the moment.

#define A 1

extern int printf(char const *, ...);

void Test(int a) {
printf("%d\n", a);
}

int main() {
Test(A); // prints 1

#undef A
#define A 2

Test(A); // prints 2

return 0;
}

Now, what we want to do is ONLY preprocess that code to see what it
produces. To do that, use cl /E whatever.c, and it'll display the
preprocessed output. That's why we got rid of stdio.h -- it will have
lots of extra "stuff" we don't care about that gets in the way of
seeing what we do care about. Here's the output I get:

#line 1 "ize.c"
extern int printf(char const *, ...);

void Test(int a) {
printf("%d\n", a);
}

int main() {
Test(1);


Test(2);

return 0;
}

One remote possibility occurs to me: what (if any) service pack have
you applied to Visual Studio? It's remotely possible that this got
fixed somewhere along the line, and you _could_ be running a version
from before its getting fixed. If so, simply applying the latest
service pack may suddenly make things work for you.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 23 '05 #10
* Bryan Parkoff:
[top-posting, quoting 100 irrelevant lines, quoting sig]


Please don't top-post in this group.

Please don't quote irrelevant stuff.

Please don't quote signatures.

As you're using Microsoft Outlook Express: please do install
OE QuoteFix.

And please, read the FAQ. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #11
Jerry,

Yes, Visual C++ 6.0 is running on Service Pack 5, but the result is
still the same. I did make few changes. Look at my example below. Do you
know that it is not working? Test() function is not executed twice to test
A1 and A2. I believe that #undef and #define are not working which it means
that it cannot define and undefine at the same time while main function is
executed. It can only be defined at one time.
It seems that there is nothing we can do to fix the problem. The
alternative option is to create multiple Test() function. First Test()
contains A1 and second Test() contains A2. It is always not flexible
without macro conditional testing before lines can be inserted or removed
inside function.
Try to compare Test() function and Test2() function. "if (a == 1)" is
removed automatically in release version during the optimization in Test2()
function. It can be inline or forceinline that it works fine, but "if (a ==
1)" will never remove in debug version because inline or forceinline is not
allowed in debug version. It is what Test() function is the option in both
debug version and release version, but it does not work this way.
Does it make sense? It is the way how I design one function with 50
lines. Each "if (a == xx)" has about 10 to 15 lines inside one function
that is total about 5 "if (a == xx)".

Bryan Parkoff
#include <stdio.h>

void Test(int a)
{
#ifdef A1
printf("1:%d\n", a);
#endif

#ifdef A2
printf("2:%d\n", a);
#endif
}

inline void Test2(int a)
{
if (a == 1)
{
printf("1\n");
//....10-15 lines....
}

if (a == 2)
{
printf("2\n");
//....10-15 lines....
}
}

int main(void)
{
#define A1
Test(1);

#undef A1
#define A2
Test(2);

return 0;
}
Bryan Parkoff

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.sunsite.dk...
In article <Z7**************@newssvr30.news.prodigy.com>,
no****@nospam.com says...
Jerry,

Yes, it still gets "1" twice. I use Microsoft Visual C++ 6.0. Do
you
know if I should be able to fix this problem by using C++ Compiler's
option?
Or...do you suggest me to use another C++ Compiler?


This seems very strange -- I originally tested it with VC++ 7.1, but
doing a quick check with 6.0, the result is identical. Just for fun,
I also tested it with Borland 5.5 and Comeau 4.3.4, all with
identical results.

Just for fun, let's try a little exercise. I've rewritten the code to
eliminate the header, and just declare printf directly -- generally a
poor idea, but helpful for the moment.

#define A 1

extern int printf(char const *, ...);

void Test(int a) {
printf("%d\n", a);
}

int main() {
Test(A); // prints 1

#undef A
#define A 2

Test(A); // prints 2

return 0;
}

Now, what we want to do is ONLY preprocess that code to see what it
produces. To do that, use cl /E whatever.c, and it'll display the
preprocessed output. That's why we got rid of stdio.h -- it will have
lots of extra "stuff" we don't care about that gets in the way of
seeing what we do care about. Here's the output I get:

#line 1 "ize.c"
extern int printf(char const *, ...);

void Test(int a) {
printf("%d\n", a);
}

int main() {
Test(1);


Test(2);

return 0;
}

One remote possibility occurs to me: what (if any) service pack have
you applied to Visual Studio? It's remotely possible that this got
fixed somewhere along the line, and you _could_ be running a version
from before its getting fixed. If so, simply applying the latest
service pack may suddenly make things work for you.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #12
Hello,

Thank you for the information and I will download OE-QuoteFix as soon as
possible. Thanks...

Bryan Parkoff

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* Bryan Parkoff:
[top-posting, quoting 100 irrelevant lines, quoting sig]


Please don't top-post in this group.

Please don't quote irrelevant stuff.

Please don't quote signatures.

As you're using Microsoft Outlook Express: please do install
OE QuoteFix.

And please, read the FAQ. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jul 23 '05 #13
In article <ps**************@newssvr12.news.prodigy.com>,
no****@nospam.com says...
Jerry,

Yes, Visual C++ 6.0 is running on Service Pack 5, but the result is
still the same.


Then post the result after preprocessing as I suggested.

Nobody can help you by simply looking at a zillion permutations of
code you claim doesn't work. If you want to fix the problem, you need
to diagnose it.

Frankly, I rather doubt you really attempted to run the code as I
posted it at all, because I can't imagine a way in which the compiler
could screw up something this simple, and still function at all. But,
if you'll post the result from preprocessing, we can see what it's
doing or not doing, and without it, you're basically just wasting my
time and everybody's bandwidth.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 23 '05 #14
* Bryan Parkoff:
[Top-posting, quoting signature]
Top-posting and quoting corrected...

* Bryan Parkoff: * Alf P. Steinbach:
* Bryan Parkoff:
[top-posting, quoting 100 irrelevant lines, quoting sig]


Please don't top-post in this group.

Please don't quote irrelevant stuff.

Please don't quote signatures.

As you're using Microsoft Outlook Express: please do install
OE QuoteFix.

And please, read the FAQ. ;-)


Thank you for the information and I will download OE-QuoteFix as soon as
possible. Thanks...


You're welcome.

Let's hope it makes it easier to quote "correctly". ;-)

Btw., most other news-clients do not suffer from OE's problems.

In time you might want to consider e.g. Forté Agent, Thunderbird, 40tude
Dialog, Gravity or XNews (they're all free).

For links, just ignore the Norwegian text if it's incomprehensible, see
<url: http://home.no.net/alfps/usenet_intro_no/setup.html#news_client>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #15
Bryan Parkoff wrote:

Jerry,

Yes, Visual C++ 6.0 is running on Service Pack 5, but the result is
still the same. I did make few changes. Look at my example below. Do you
know that it is not working? Test() function is not executed twice to test
A1 and A2. I believe that #undef and #define are not working
No. It means that your understanding of what #define and #undefine
does is serously flawed.
Please get a good book on programming in C++ and study it!

#include <stdio.h>

void Test(int a)
{
#ifdef A1
printf("1:%d\n", a);
#endif

#ifdef A2
printf("2:%d\n", a);
#endif
You are epxecting that this function is adpapted to either
print '1' or '2' depending what definement is active at the
time the function is called in main(). But this is *not* what
is going to happen.
#ifdef serves for a different problem and has nothing to do
with what you want to achieve. You are simply using the wrong
tool for your work. That's all.
If you want to make decissions during runtime, then plain-vanilla
'if' is your tool. If you want the compiler to do different things
during compilation depending on some environt 'variables' and to
modify the actual source code before it gets compiled then #ifdef
is your tool.
}

inline void Test2(int a)
{
if (a == 1)
{
printf("1\n");
//....10-15 lines....
}

if (a == 2)
{
printf("2\n");
//....10-15 lines....
}
}

int main(void)
{
#define A1
Test(1);

#undef A1
#define A2
Test(2);

return 0;
}


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #16
Bryan Parkoff <no****@nospam.com> wrote:
|

Please read this:
<URL: http://alt-usage-english.org/posting_quotes.html>

| I want to let you know that your example code does not work. If
| template function reads "1" and "2", then it will execute "2" twice. It
| never shows "1" before "2". Look at my example that is similiar yours.

It does on my computer, using intel c++ 8.1 and gnu c++ 3.4.3.

| #include <stdio.h>
|
| template <int A>
| void Test(void)
| {
| if (A == 1)
| printf("1\n");
| if (A == 2)
| printf("2\n");
| if (A == 3)
| printf("3\n");
| printf("\n");
| }
|
| int main(void)
| {
| Test<1>();
| Test<2>();
| Test<3>();
|
| return 0;
| }
|
| Output shows "3" in first line, second line, and third line. Is there a
| fix?

Your compiler, your computer, or you are broken. You must find out what
is broken first, then you can fix.

[snip quotes]
--
Robert Bauck Hamar
Jul 23 '05 #17
Bryan Parkoff <no****@nospam.com> wrote:
| Robert Bauck Hamar,
|
| I want to say thank you very much for the information. I try to add
| comment so you can understand better. Please look at my example. It has
| multiple functions. They are almost identical. Sometimes, I use B++, but
| sometimes, I use C++. I do not wish to use Test1(), Test2(), and Test3(),
| but I use one function as Test(). I expect that Test1(), Test2(), and
| Test3() stop to exist in the source code. Please read second paragraph
| below my example.
|
| void Test(void)
| {
| A++;

In your first post, A was a macro.

| B++;
| C++;
| }
|
| void Test1(void)
| {
| A++;
| }
|
| void Test2(void)
| {
| A++;
| B++;
| }
|
| void Test3(void)
| {
| A++;
| C++;
| }

Please read a decent book on C++, then try again.

| If Test() contains "if (A == XX)", it should never exist in the machine
| language object in both debug version and release version. I expect C++

Why do you bother with the debug version? You generally want the debug
version to be 'debugable.'

| compiler to remove "if (A == XX)" before A++, B++, and/or C++ can be stored

If A had been a compile time constant, that might have happened, but now
you want to modify A.

| in the machine language object. Look at my example below. Please read
| third paragraph below my example.
|
| int main(void)
| {
| A = 1;
| Test(); // Put A++ only (B++ and C++ are excluded) without "if (A ==
| XX)"
| A = 2;
| Test(); // Put A++ and B++ only (C++ is excluded) without "if (A == XX)"
| A = 3;
| Test(); // Put A++ and C++ only (B++ is excluded) without "if (A == XX)"
| A = 0;
| Test(); // Put A++, B++, and C++ without "if (A == XX)"
| }
|
| void Test(void)
| {
| if (A == 1)
| A++;
| if (A == 2)
| B++;
| if (A == 3)
| C++;
| }

Your code doesn't match the comments in it. The first time around, I
tried to guess what you wanted, but I can't possibly guess your
intentions here.

| After my C++ source code is compiled into machine language object in
| both debug version and release version, "if (A == XX)" is removed
| automatically, A++, B++, and/or C++ can be preserved or removed. It would
| reduce x86 instructions and improve performance.

If the test expression in the if statement is constant at compile time,
I'd expect just about any compiler to optimize the test away, but only
if optimizations are turned on. Why do you need this to take place in
your compiler's 'debug version?'

I compiled my first example to assembler code, and checked out what the
compilers did. The result was: The gnu compiler removed both tests and
the body of the failing if. The intel compiler also inlined the
function, efficiently reducing the whole program to two printf calls.

| I know that "if (A == XX)" do not work so it needs one or more variables
| like "if (B == XX)" and "if (C == XX)" so they can test correctly.

Huh?

| Do you expect that template is only the answer?

What are you actually trying to achieve? Have you actually tested some
code and found it to be to slow? If not, first try to make your code
correct, then if it is to slow, use a profiler to find out where it is
slow.
--
Robert Bauck Hamar
Jul 23 '05 #18
"Bryan Parkoff" <no****@nospam.com> wrote in message
news:qd*************@newssvr11.news.prodigy.com :
"#define" can only be inside the global scope
before main() function.
No. #define is not part of the C++ language.
It's part of the "C Preprocessor Language".
It cannot have or be-in a "scope". It can appear
anywhere in a C or C++ file (or, indeed, any text
file you wish to feed into a C preprocessor),
on a line by itself (and preferably at the start
of the line), and it's effective "scope" is from
immediately below it to the end of the file in
which it appears. (Unless, lower down the page,
you #undef the thing you #defined above.)

Hint: #define, #ifdef, #ifndef, #include, etc.
are not in any way related to any programming
language. They're just automated text editing
commands for selective cutting and pasting of
text. That is ALL they do.

Most C++ experts don't like 'em, tend to steer
people away from using them whenever possible.

Myself, I love 'em. I use macros, function-like
macros, #defines, #ifdefs, etc. a lot. I don't
see that they've lost their usefulness at all
over the years. I suppose that makes me
anachronistic. Oh, well. :-)

Re.:
... If there is no way to test it at compile time ...


Sure there is. Most compilers have some sort of
switch that will allow you to just preprocess your
input file, and save the preprocessed (but
uncompiled) version to a text file. That way you
can see what your preprocessor directives are
really doing. (Hint: remove any #includes of
large header files before testing if you don't
want to wade through a half-million lines of
gunk.) Consult your compiler manual for
further details.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
15
by: Jorge Naxus | last post by:
Hello I would like to write a macro like that: #ifdef DEBUG #define printj(...) printf(...) #else #printj(...) #endif
3
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a...
19
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
12
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each...
4
by: rasmidas | last post by:
There are two different directories. sparse/src/stk/dmg_apiutil and sparse/src/stk/dmg_meta_doc Inside these two directories there are lot of C++ files. I have written a function:
2
by: jiuguangw | last post by:
Hi, I currently have a application which needs to dynamically define a function (post-compilation). In Lisp, this can be done by embedding a function definition inside a string, and call...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.