473,651 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Evaluating Preprocessor

We might use the normal preprocessor something like this

#define WIDTH 42
#define HEIGHT 100
#define AREA (WIDTH*HEIGHT)

To have "42*100" is inserted in the code whereever AREA is used. But
does an evaluating preprocessor exist that can do some evaluation on
expressions so that AREA becomes 4200?

I know that most compilers will figure this out themselves anyway.

And I know that I should use a const instead for this kind of thing.

But in general (for instance with old (C) compilers that don't have
const, or compilers that don't optimize very well), it would be nice to
_know_ that an expression is evaluated before compile-time and runtime.
In general, it would be nice to have a simple and clean turing complete
language for compile time evaluation. I guess I can easily make a
simple but powerful preprocessor in Python that can do this, also for
C/C++ code. But maybe it already exist?

/David

Nov 10 '06 #1
4 2082
pi************@ gmail.com wrote:
We might use the normal preprocessor something like this

#define WIDTH 42
#define HEIGHT 100
#define AREA (WIDTH*HEIGHT)
But hopefully you won't.
To have "42*100" is inserted in the code whereever AREA is used. But
does an evaluating preprocessor exist that can do some evaluation on
expressions so that AREA becomes 4200?
No, although the preprocessor does evaluate arithmatic expressions in
some contexts (such as, for an #if directive), most of the time it
merely performs textual substitution.
I know that most compilers will figure this out themselves anyway.

And I know that I should use a const instead for this kind of thing.
Then why not use const? Doing so would guarantee a compile-time
evaluation of 42 * 100:

const int kWidth = 42;
const int kHeight = 100;
const int kArea = 42 * 100;

Since the compiler needs a constant value for the symbol "kArea" it has
no choice but to multiply 42 by 100 at compile time.
But in general (for instance with old (C) compilers that don't have
const, or compilers that don't optimize very well), it would be nice to
_know_ that an expression is evaluated before compile-time and runtime.
This is a C++ newsgroup, so it seems odd to be asking the best practice
for languages other than C++.

And if you want to know whether 42 * 100 is evaluated at runtime or at
compile time, then just examine a disassembly of a compiled binary to
find out. Alternately you could ask your compiler maker whether the
compiler performs "constant folding" as an optimization. And if it
doesn't, find a better compiler.
In general, it would be nice to have a simple and clean turing complete
language for compile time evaluation. I guess I can easily make a
simple but powerful preprocessor in Python that can do this, also for
C/C++ code. But maybe it already exist?
I fervently hope that you do have better uses for your time than to
rewrite the C++ preprocessor. Besides, it has been done, only it's
called "C++ template metaprogramming ".

template <int Nstruct Height
{ static const int value = N; };

template <int Nstruct Width
{ static const int value = N; };

template <class H, class W>
struct Area
{
static const int value = H::value * W::value;
};

Area<Height<42> , Width<100::valu e; // 4200

It's possible to perform much more sophisticated (though not
necessarily any less verbose) compile-time calculations with C++
metaprogramming . At least, for those so inclined.

Greg

Nov 10 '06 #2
#define WIDTH 42
#define HEIGHT 100
#define AREA (WIDTH*HEIGHT)
But hopefully, cooler heads will prevail.
To have "42*100" is inserted in the code whereever AREA is used. But
does an evaluating preprocessor exist that can do some evaluation on
expressions so that AREA becomes 4200?
No, although the preprocessor does evaluate arithmatic expressions in
some contexts (such as, for an #if directive), most of the time it
merely performs textual substitution.
I know that most compilers will figure this out themselves anyway.
And I know that I should use a const instead for this kind of thing.
Then why not use const? Doing so would guarantee a compile-time
evaluation of 42 * 100:

const int kWidth = 42;
const int kHeight = 100;
const int kArea = kWidth * kHeight;

Since the compiler needs a constant value for the symbol "kArea" it has

no choice but to multiply 42 by 100 at compile time.
But in general (for instance with old (C) compilers that don't have
const, or compilers that don't optimize very well), it would be nice to
_know_ that an expression is evaluated before compile-time and runtime.
This is a C++ newsgroup, so it seems odd to be asking the best practice

for languages other than C++.

And if you want to know whether 42 * 100 is evaluated at runtime or at
compile time, then just examine a disassembly of a compiled binary to
find out. Alternately you could ask your compiler maker whether the
compiler performs "constant folding" as an optimization. And if it
doesn't, find a better compiler.
In general, it would be nice to have a simple and clean turing complete
language for compile time evaluation. I guess I can easily make a
simple but powerful preprocessor in Python that can do this, also for
C/C++ code. But maybe it already exist?
I fervently hope that you do have better uses for your time than to
rewrite the C++ preprocessor. Besides, it has been done, only it's
called "C++ template metaprogramming ".

template <int Nstruct Height
{ static const int value = N; };

template <int Nstruct Width
{ static const int value = N; };

template <class H, class W>
struct Area
{
static const int value = H::value * W::value;
};

Area<Height<42> , Width<100::valu e; // 4200

It's possible to perform much more sophisticated (though not
necessarily any less verbose) compile-time calculations with C++
metaprogramming . At least, for those so inclined.

Greg

Nov 10 '06 #3
We might use the normal preprocessor something like this
#define WIDTH 42
#define HEIGHT 100
#define AREA (WIDTH*HEIGHT)
But hopefully, cooler heads will prevail.
To have "42*100" is inserted in the code whereever AREA is used. But
does an evaluating preprocessor exist that can do some evaluation on
expressions so that AREA becomes 4200?
No, although the preprocessor does evaluate arithmatic expressions in
some contexts (such as, for an #if directive), most of the time it
merely performs textual substitution.
I know that most compilers will figure this out themselves anyway.
And I know that I should use a const instead for this kind of thing.
Then why not use const? Doing so would guarantee a compile-time
evaluation of 42 * 100:

const int kWidth = 42;
const int kHeight = 100;
const int kArea = kWidth * kHeight;

Since the compiler needs a constant value for the symbol "kArea" it has
no choice but to multiply 42 by 100 at compile time.
But in general (for instance with old (C) compilers that don't have
const, or compilers that don't optimize very well), it would be nice to
_know_ that an expression is evaluated before compile-time and runtime.
This is a C++ newsgroup, so it seems odd to be asking the best practice
for languages other than C++.

And if you want to know whether 42 * 100 is evaluated at runtime or at
compile time, then just examine a disassembly of a compiled binary to
find out. Alternately you could ask your compiler maker whether the
compiler performs "constant folding" as an optimization. And if it
doesn't, find a better compiler.
In general, it would be nice to have a simple and clean turing complete
language for compile time evaluation. I guess I can easily make a
simple but powerful preprocessor in Python that can do this, also for
C/C++ code. But maybe it already exist?
I fervently hope that you do have better uses for your time than to
rewrite the C++ preprocessor. Besides, it has been done, only it's
called "C++ template metaprogramming ".

template <int Nstruct Height
{ static const int value = N; };

template <int Nstruct Width
{ static const int value = N; };

template <class H, class W>
struct Area
{
static const int value = H::value * W::value;
};

Area<Height<42> , Width<100::valu e; // 4200

It's possible to perform much more sophisticated (though not
necessarily any less verbose) compile-time calculations with C++
metaprogramming . At least, for those so inclined.

Greg

Nov 10 '06 #4
pi************@ gmail.com wrote:
>We might use the normal preprocessor something like this
#define WIDTH 42
#define HEIGHT 100
#define AREA (WIDTH*HEIGHT)
But hopefully, cooler heads will prevail.
To have "42*100" is inserted in the code whereever AREA is used. But
does an evaluating preprocessor exist that can do some evaluation on
expressions so that AREA becomes 4200?
No, although the preprocessor does evaluate arithmatic expressions in
some contexts (such as, for an #if directive), most of the time it
merely performs textual substitution.
I know that most compilers will figure this out themselves anyway.
And I know that I should use a const instead for this kind of thing.
Then why not use const? Doing so would guarantee a compile-time
evaluation of 42 * 100:

const int kWidth = 42;
const int kHeight = 100;
const int kArea = kWidth * kHeight;

Since the compiler needs a constant value for the symbol "kArea" it has
no choice but to multiply 42 by 100 at compile time.
But in general (for instance with old (C) compilers that don't have
const, or compilers that don't optimize very well), it would be nice to
_know_ that an expression is evaluated before compile-time and runtime.
This is a C++ newsgroup, so it seems odd to be asking the best practice
for languages other than C++.

And if you want to know whether 42 * 100 is evaluated at runtime or at
compile time, then just examine a disassembly of a compiled binary to
find out. Alternately you could ask your compiler maker whether the
compiler performs "constant folding" as an optimization. And if it
doesn't, find a better compiler.
In general, it would be nice to have a simple and clean turing complete
language for compile time evaluation. I guess I can easily make a
simple but powerful preprocessor in Python that can do this, also for
C/C++ code. But maybe it already exist?
I fervently hope that you do have better uses for your time than to
rewrite the C++ preprocessor. Besides, it has been done, only it's
called "C++ template metaprogramming ".

template <int Nstruct Height
{ static const int value = N; };

template <int Nstruct Width
{ static const int value = N; };

template <class H, class W>
struct Area
{
static const int value = H::value * W::value;
};

Area<Height<42> , Width<100::valu e; // 4200

It's possible to perform much more sophisticated (though not
necessarily any less verbose) compile-time calculations with C++
metaprogramming . At least, for those so inclined.

Greg

Nov 10 '06 #5

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

Similar topics

205
10590
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
40176
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was looking to write something along the lines of:
16
4486
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the following example. eg cosider 2 files sample.c and sample.h sample.h
18
3021
by: /* frank */ | last post by:
My teacher said that array in C is managed by preprocessor. Preprocesser replace all array occurences (i.e. int a ) with something that I don't understand/remember well. What's exactly happens with array during preprocessing/compiling stage? Thanks in advance
13
2117
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int defined = 2;
9
3651
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor phase only. I am not using the standalone program 'cpp' because proper functioning of my project...
2
6634
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0 where the definitions were: _UNICODE,_DEBUG,_WIN32_DCOM,WIN32,_WINDOWS,_WINDLL,_AFXDLL,_USRDLL In VC++7.1, these are the preprocessor definitions of the project (right-click the project in Solution Explorer and choose Properties -> C++ ->...
32
2772
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input the same file ? If not then how much variation is allowed ? Is it just a bit more or less white space here and there or could could there be larger differences ? If the output is not deterministic then is it possible that the output of the...
31
2907
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.