Hi, I have some code like this:

if ( a < 99999 ) {
do_something();
}

Is there some way to modify it like this:

#define NUMBER_OF_9 5

if ( a < (power(10,NUMBER_OF_9) - 1) ) {
do_something();
}

I need the power() function to be extended while pre-compile, and
generate code like the first peice for compiler.

Or, Is the complier smart enough to compute the exponent since the two
parameter are all const?

_int64( 1e5 ) by Jeff_Relf

Jeff_Relf
Mon Oct 22 21:42:03 PDT 2007

You can do something like this:

__int64 X;
....
if ( X < __int64( 1e5 ) ) do_something();
// or... 2^48 â?? << â?? happens before â?? < â??.
if ( X < __int64( 1 ) << 48 )
do_something();



Re: _int64( 1e5 ) by sevenever

sevenever
Mon Oct 22 22:01:49 PDT 2007

On Oct 23, 12:42 pm, Jeff Relf <Jeff_R...@Yahoo.COM> wrote:
> You can do something like this:
>
> __int64 X;
> ....
> if ( X < __int64( 1e5 ) ) do_something();
> // or... 2^48 " << " happens before " < ".
> if ( X < __int64( 1 ) << 48 )
> do_something();

I need generate the 99999 by the const NUMBER_OF_9


Re: Can I get some code executed while pre-compile? by Alexander

Alexander
Mon Oct 22 22:20:05 PDT 2007

How about:

#define STRING_OF_NINES_proxy(n) (1E ## n - 1)
#define STRING_OF_NINES(n) STRING_OF_NINES_proxy(n)

"sevenever" <sevenever@gmail.com> wrote in message
news:1193113820.684236.120560@v29g2000prd.googlegroups.com...
> Hi, I have some code like this:
>
> if ( a < 99999 ) {
> do_something();
> }
>
> Is there some way to modify it like this:
>
> #define NUMBER_OF_9 5
>
> if ( a < (power(10,NUMBER_OF_9) - 1) ) {
> do_something();
> }
>
> I need the power() function to be extended while pre-compile, and
> generate code like the first peice for compiler.
>
> Or, Is the complier smart enough to compute the exponent since the two
> parameter are all const?
>



SevenEver, try: =?UTF-8?Q?=E2=80=9C?= #define Nines( Exp ) __int64( 1e##Exp ) =?UTF-8?Q?=E2=80=9D.?= by Jeff_Relf

Jeff_Relf
Mon Oct 22 22:23:19 PDT 2007

You can do something like this, SevenEver:

#define Nines( Exp ) __int64( 1e##Exp )

__int64 X;
....
if ( X < Nines( 5 ) ) do_something();



Re: Can I get some code executed while pre-compile? by Tim

Tim
Mon Oct 22 22:46:16 PDT 2007

sevenever <sevenever@gmail.com> wrote:
>
>Hi, I have some code like this:
>
>if ( a < 99999 ) {
> do_something();
>}
>
>Is there some way to modify it like this:
>
>#define NUMBER_OF_9 5
>
>if ( a < (power(10,NUMBER_OF_9) - 1) ) {
> do_something();
>}
>
>I need the power() function to be extended while pre-compile, and
>generate code like the first peice for compiler.

The pre-processor is really just a text substitution engine, and little
more. However, you could do this:

#define NINES_1 9
#define NINES_2 99
#define NINES_3 999
#define NINES_4 9999
#define NINES_5 99999
#define NINES(x) NINES_##x

Then you could wrote
if( a < NINES(5) ) {
but I'm not sure that's better than
if( a < NINES_5 ) {

>Or, Is the complier smart enough to compute the exponent since the two
>parameter are all const?

No. For one thing, there is no "power" function in C that accepts integer
parameters and produces an integer result. Even if there were, the "power"
function wouldn't have any meaning to the compiler. It's just another
function that accepts two parameters and returns an integer. It can't be
optimized away. The compiler does have a list of "intrinsic" functions
that get special treatment, but "power" probably wouldn't be one of them.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: Can I get some code executed while pre-compile? by Ben

Ben
Tue Oct 23 08:56:53 PDT 2007


"sevenever" <sevenever@gmail.com> wrote in message
news:1193113820.684236.120560@v29g2000prd.googlegroups.com...
> Hi, I have some code like this:
>
> if ( a < 99999 ) {
> do_something();
> }
>
> Is there some way to modify it like this:
>
> #define NUMBER_OF_9 5
>
> if ( a < (power(10,NUMBER_OF_9) - 1) ) {
> do_something();
> }

Yes: using templates and a specialization

template <int N>
struct NTH9
{
enum { value = 10 * NTH9<N-1>::value + 9 };
};

template<>
struct NTH9<0>
{
enum { value = 0 };
};

if (a < NTH9<NUMBER_OF_9>::value)
do_something();



Re: Can I get some code executed while pre-compile? by Giovanni

Giovanni
Tue Oct 23 09:27:12 PDT 2007


"Ben Voigt [C++ MVP]" <rbv@nospam.nospam> ha scritto nel messaggio
news:OV8e$0YFIHA.1168@TK2MSFTNGP02.phx.gbl...

> Yes: using templates and a specialization
> [...]

Wow... this is pure C++ template black-magic :)
( I stopped at the preprocessor level :( )

Congrats.

Giovanni




Re: Can I get some code executed while pre-compile? by Alexander

Alexander
Tue Oct 23 10:26:38 PDT 2007

No, it's just regular (and rather simple) template metaprogramming.
I'd have posted it myself if Ben hadn't beat me to it. However,
personally I avoid it as it's a pain to review...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Giovanni Dicanio" <giovanni.dicanio@invalid.it> wrote in message
news:OBzNwGZFIHA.6120@TK2MSFTNGP05.phx.gbl...
>
> "Ben Voigt [C++ MVP]" <rbv@nospam.nospam> ha scritto nel messaggio
> news:OV8e$0YFIHA.1168@TK2MSFTNGP02.phx.gbl...
>
>> Yes: using templates and a specialization
>> [...]
>
> Wow... this is pure C++ template black-magic :)
> ( I stopped at the preprocessor level :( )
>
> Congrats.
>
> Giovanni
>
>
>



Re: Can I get some code executed while pre-compile? by Giovanni

Giovanni
Tue Oct 23 10:59:24 PDT 2007


"Alexander Nickolov" <agnickolov@mvps.org> ha scritto nel messaggio
news:ujnzDnZFIHA.4748@TK2MSFTNGP06.phx.gbl...
> No, it's just regular (and rather simple) template metaprogramming.

No, I think that "simple" is in the eye of the beholder :)

BTW: We had some discussions about template metaprogramming in the MFC
newsgroup; I personally can't "grok" that kind of code, and I'm not alone.

For example, there was a discussion also about the countof() template, I
stop at countof made using the preprocess, i.e.

#define countof( a ) ( sizeof(a) / ( sizeof( (a)[0] ) )

but there is some template metaprogramming "magic" that allows to write a
countof which gives a compiler error if you pass a pointer to it (instead of
array).

I use "moderate C++" :) , not "Boost-level" template-metaprogramming C++.

Giovanni





Re: Can I get some code executed while pre-compile? by Igor

Igor
Tue Oct 23 11:19:50 PDT 2007

Giovanni Dicanio <giovanni.dicanio@invalid.it> wrote:
> For example, there was a discussion also about the countof()
> template, I stop at countof made using the preprocess, i.e.
>
> #define countof( a ) ( sizeof(a) / ( sizeof( (a)[0] ) )
>
> but there is some template metaprogramming "magic" that allows to
> write a countof which gives a compiler error if you pass a pointer to
> it (instead of array).

Something like this, I guess:

template <typename T, size_t N>
char (& countof_helper(T (&)[N]) )[N];

#define countof(a) sizeof(countof_helper(a))


countof_helper is a function taking a reference to an array of any type
and any size, and returning a reference to an array of char of the same
size. The function doesn't need to be implemented, just declared. sizeof
only cares about the type of the expression, but doesn't actually
evaluate the expression.

Passing anything other than an array to countof_helper would fail
template parameter deduction, and you would get a compiler error.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Can I get some code executed while pre-compile? by Alex

Alex
Tue Oct 23 11:41:56 PDT 2007

"Giovanni Dicanio" wrote:
> For example, there was a discussion also about the
> countof() template, I stop at countof made using the
> preprocess, i.e.
>
> #define countof( a ) ( sizeof(a) / ( sizeof( (a)[0] ) )
>
> but there is some template metaprogramming "magic" that
> allows to write a countof which gives a compiler error if
> you pass a pointer to it (instead of array).

There is no template metaprogramming "magic". It's just an
ugly syntax, that's all. You can see an example of such
`countof' macro in "winnt.h" header. Look for `ARRAYSIZE'
macro. There is long detailed explanation of how this macro
work with examples.

Also, I suggest you to read this article:

"Interpreting More Complex Declarators"
http://msdn2.microsoft.com/en-us/library/ms859264.aspx

Alex



Re: Can I get some code executed while pre-compile? by Giovanni

Giovanni
Tue Oct 23 14:52:29 PDT 2007


"Alex Blekhman" <tkfx.REMOVE@yahoo.com> ha scritto nel messaggio
news:evKlaUaFIHA.3548@TK2MSFTNGP06.phx.gbl...

> There is no template metaprogramming "magic". It's just an ugly syntax,
> that's all.
[...]
> Also, I suggest you to read this article:
>
> "Interpreting More Complex Declarators"
> http://msdn2.microsoft.com/en-us/library/ms859264.aspx

Thanks for the article URL.

Giovanni



Re: Can I get some code executed while pre-compile? by Ben

Ben
Tue Oct 23 15:40:08 PDT 2007


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:uqGUiMaFIHA.3940@TK2MSFTNGP05.phx.gbl...
> Giovanni Dicanio <giovanni.dicanio@invalid.it> wrote:
>> For example, there was a discussion also about the countof()
>> template, I stop at countof made using the preprocess, i.e.
>>
>> #define countof( a ) ( sizeof(a) / ( sizeof( (a)[0] ) )
>>
>> but there is some template metaprogramming "magic" that allows to
>> write a countof which gives a compiler error if you pass a pointer to
>> it (instead of array).
>
> Something like this, I guess:
>
> template <typename T, size_t N>
> char (& countof_helper(T (&)[N]) )[N];

or

template <typename T, size_t N>
size_t countof(T (&)[N])
{
return N;
}

which needs no macro (but isn't an "integral constant").

>
> #define countof(a) sizeof(countof_helper(a))
>
>
> countof_helper is a function taking a reference to an array of any type
> and any size, and returning a reference to an array of char of the same
> size. The function doesn't need to be implemented, just declared. sizeof
> only cares about the type of the expression, but doesn't actually evaluate
> the expression.
>
> Passing anything other than an array to countof_helper would fail template
> parameter deduction, and you would get a compiler error.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>



I wouldn't touch their code with a ten foot pole. by Jeff_Relf

Jeff_Relf
Tue Oct 23 19:04:13 PDT 2007

So Alexander, Giovanni and Ben_Voigt would use
â?? regular ( and rather simple ) template metaprogramming â??
instead of just â?? if ( X < 1e5 ) â?? ? !

I'm shocked.
I wouldn't touch their code with a ten foot pole.


Re: I wouldn't touch their code with a ten foot pole. by Giovanni

Giovanni
Wed Oct 24 01:10:12 PDT 2007


"Jeff?Relf" <Jeff_Relf@Yahoo.COM> ha scritto nel messaggio
news:Jeff_Relf_2007_Oct_23__7_4_PM@Cotse.NET...
> So Alexander, Giovanni and Ben_Voigt would use
> " regular ( and rather simple ) template metaprogramming "
> instead of just " if ( X < 1e5 ) " ? !
>
> I'm shocked.
> I wouldn't touch their code with a ten foot pole.

No, I don't master C++ at so advanced level like Ben, Alexander, Igor; as I
wrote above, I can't master template metaprogramming like them.

But I do respect those men having a so deep knowledge of C++.

I can't touch or modify the above template metaprogramming code; if I have
code like this, I would just consider it as a "black-box". This is the same
for STL: I learnt to appreciate STL power and elegant design, but I couldn't
write an STL implementation from scratch.

For me, templates are a good *tool* for generic containers ( Container< T
> ); I know also the template traits, but I can't master advanced template
metaprogramming like the countof sample.

Giovanni



Re: I wouldn't touch their code with a ten foot pole. by Alex

Alex
Wed Oct 24 02:59:55 PDT 2007

"Jeff?Relf" wrote:
> So Alexander, Giovanni and Ben_Voigt would use
> " regular ( and rather simple ) template metaprogramming "
> instead of just " if ( X < 1e5 ) " ? !
>
> I'm shocked.
> I wouldn't touch their code with a ten foot pole.

The one thing you just don't understand is that there are
projects with more than one file and more than one developer
involved.

Alex




Re: I wouldn't touch their code with a ten foot pole. by Alex

Alex
Wed Oct 24 03:57:31 PDT 2007

"Giovanni Dicanio" wrote:
> I can't touch or modify the above template metaprogramming
> code; if I have code like this, I would just consider it
> as a "black-box".

Actually, most of the time people have difficulties with
understanding something because of the lack of good
explanation, and not because of learning material
complexity. The template metaprogramming is one of these
topics. I think it's actually simpler than preprocessor
intricacies.

Alex



Open Source == drawn-out flame wars. by Jeff_Relf

Jeff_Relf
Wed Oct 24 05:02:36 PDT 2007

I don't know what you think a â?? developer â?? is, Alex_Blekhman,
but I was working with teams of programmers starting in
1982 ( on the Lilith, a Modula-2 engine ).

I've held my current â?? gig â?? for the last 14 years.
Directly, I'm the only one who makes changes to the .CPP files.
Indirectly, many others ( including coders ) makes changes, of course.

A consortium of banking schools spent lot of time ( 3 years )
and money ( ¿ 100 thousand U.S. dollars ? )
trying to make some of my code â?? Open Source â??, kind of like WikiPedia.

Some MicroSofties worked on it. ( I live in the U-District, Seattle )

Many in the consortium feel that they'll get free sofware this way
and, to boot, they think they'll gain greater control of the code.

But, unfortunately for them, they don't realize that,
under these conditions, hierarchies naturally arise
to squelch the unavoidable: drawn-out flame wars.

Try making a change to a controversial part of WikiPedia.
Then come back in a week or two and tell me if it's still there.

Although they managed tp unwind some macros without breaking the code,
they couldn't make any improvements... so they failed, I say.

Recently, I gave them a ton of fixes/improvements for free,
but, if they have any requests, my team will have to bill them for it,
I'm sure... as I doubt they could do it themselves.

Even if the consortium has no requests,
I've got a lot on my plate right now, working for others.


Re: I wouldn't touch their code with a ten foot pole. by Ben

Ben
Wed Oct 24 07:00:11 PDT 2007


"Jeff?Relf" <Jeff_Relf@Yahoo.COM> wrote in message
news:Jeff_Relf_2007_Oct_23__7_4_PM@Cotse.NET...
> So Alexander, Giovanni and Ben_Voigt would use
> " regular ( and rather simple ) template metaprogramming "
> instead of just " if ( X < 1e5 ) " ? !
>
> I'm shocked.
> I wouldn't touch their code with a ten foot pole.
>

I posted the template meta-version because it answers the poster's original
question "Can I get some code executed [sic] while pre-compile?". It is a
general concept while can be applied to perform compile-time computation of
many different problems.

Yes, building a numeric literal is more straightforward in this particular
instance. There's always a tradeoff between generality and simplicity.



Re: Open Source == drawn-out flame wars. by Alex

Alex
Wed Oct 24 07:21:12 PDT 2007

"Jeff?Relf" wrote:
> I don't know what you think a " developer " is,
> Alex_Blekhman,
> but I was working with teams of programmers starting in
> 1982 ( on the Lilith, a Modula-2 engine ).

So, it's quite a pity that for 25 years of programming you
didn't get that technologies develop in order to answer
everyday needs and not because of some evil mastermind.

To remind you, this thread started from the qustion how to
replace hardcoded value in a loop with something more
flexible. There were several suggestions including templates
metaprogramming (which may be very useful for certain tasks,
BTW).

You answered that you "wouldn't touch [such] code with a ten
foot pole" and would use hardcoded constant. Also, I noticed
that you constantly propose similar "solutions" (hardcoded
constants, full paths for include headers, code that relies
on internal representation of a data, etc.) to other
posters.

All this indicates that you write and maintain all of your
code alone (or just don't bother to maintain it). Because if
people listened to you and followed your advices, then their
code would become maintenance nightmare.

Alex



Re: Open Source == drawn-out flame wars. by Ulrich

Ulrich
Wed Oct 24 07:27:07 PDT 2007

Jeffâ? Relf wrote:
> Directly, I'm the only one who makes changes to the .CPP files.
> Indirectly, many others ( including coders ) makes changes, of course.

Ahem, Jeff, your code (at least what you regularly show here) is pretty much
unreadable (except to you, I guess), often invokes undefined behaviour
(according to C++) and laughs at probably every coding guideline out there
(maybe except your own). This could be okay when in a limited environment
like one person working on the code and one or a few compilers as targets.
However, this fails at portability across platforms and maintainability
when in a team where several people work on the code. Let me guess, in
spite of those 'indirectly' making changes (whatever that is supposed to
mean), you are the only one capable of making changes, right?

> A consortium of banking schools [....]

Nice story, the point of which is? I have a team here and there is dissent
sometimes. People live with that and are productive still. Anyway, you
already made clear before that you are an OSS hater, no use repeating it.

Sorry, Jeff, but the thing you still don't seem to get is that it's not only
computers for which some code is written. In fact when you work in a team,
you will find that code is written for people. Sadly you don't grok the
'people' part at all, estranging them one after the other on your way.

I'm sorry.

Uli