Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old June 25th, 2006, 03:15 PM
Frederick Gotham
Guest
 
Posts: n/a
Default Brainstorm for compile-time constants


I don't like macros for a few reasons -- here's just a few...

Reason 1:

namespace ArbitraryNamespace {

#define Func(i) ( i + '0' )

}

int Func( int const b );


Reason 2:

#define Square(a) (a*a)

int main()
{
int i = 7;

Square(i++);
}


Therefore, wherever possible, I use an alternative (e.g. templates,
typedef's, enum's, global const variables).


The following code doesn't compile because a function call can never
evaluate to a compile time constant:


unsigned TwoPowerX( unsigned const x )
{
return 1U << x;
}


int main()
{
int array[ TwoPowerX(5) ];
}



I believe it was Alf P. Steinbach who devised a suitable alternative:

template<unsigned x>
struct TwoPowerX {

static unsigned const val = 1U << x;

};


int main()
{
int array[ TwoPowerX<5>::val ];
}


Yes, this does the trick, but in my view, it isn't quite optimal because
we don't have our domestic function-call syntax.

Has anyone got any ideas as to how we could achieve this whilst retaining
our function-call syntax? (Without using a macro of course!)

As a side note, depending on the outcome of this thread, I may make a
suggestion to comp.std.c++ to add the following functionality to the
language, which might make use of the "switch" keyword:

switch TwoPowerX( unsigned const x ) return 1U << x;

int main()
{
int array[ TwoPowerX(5) ];
}


The "switch" keyword before the function definition would indicate that
it can evaluate to a compile-time constant.


--

Frederick Gotham
  #2  
Old June 25th, 2006, 09:45 PM
Howard Hinnant
Guest
 
Posts: n/a
Default Re: Brainstorm for compile-time constants

In article <Xns97ED9BBBBBAE8fgothamNOSPAM@194.125.133.14>,
Frederick Gotham <fgothamNO@SPAM.com> wrote:
[color=blue]
> As a side note, depending on the outcome of this thread, I may make a
> suggestion to comp.std.c++ to add the following functionality to the
> language, which might make use of the "switch" keyword:
>
> switch TwoPowerX( unsigned const x ) return 1U << x;
>
> int main()
> {
> int array[ TwoPowerX(5) ];
> }[/color]

See:

http://www.open-std.org/jtc1/sc22/wg...2003/n1521.pdf

for a similar proposal,

and:

http://www.open-std.org/jtc1/sc22/wg...006/n1969.html

for the status of N1521.

-Howard
  #3  
Old June 25th, 2006, 10:45 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Brainstorm for compile-time constants

Frederick Gotham wrote:
[color=blue]
>
> I don't like macros for a few reasons -- here's just a few...
>
> Reason 1:
>
> namespace ArbitraryNamespace {
>
> #define Func(i) ( i + '0' )
>
> }
>
> int Func( int const b );
>
>
> Reason 2:
>
> #define Square(a) (a*a)
>
> int main()
> {
> int i = 7;
>
> Square(i++);
> }
>
>
> Therefore, wherever possible, I use an alternative (e.g. templates,
> typedef's, enum's, global const variables).
>
>
> The following code doesn't compile because a function call can never
> evaluate to a compile time constant:
>
>
> unsigned TwoPowerX( unsigned const x )
> {
> return 1U << x;
> }
>
>
> int main()
> {
> int array[ TwoPowerX(5) ];
> }
>
>
>
> I believe it was Alf P. Steinbach who devised a suitable alternative:
>
> template<unsigned x>
> struct TwoPowerX {
>
> static unsigned const val = 1U << x;
>
> };
>
>
> int main()
> {
> int array[ TwoPowerX<5>::val ];
> }
>
>
> Yes, this does the trick, but in my view, it isn't quite optimal because
> we don't have our domestic function-call syntax.
>
> Has anyone got any ideas as to how we could achieve this whilst retaining
> our function-call syntax? (Without using a macro of course!)[/color]

I don't think that's possible. AFAICS, the only things that can be used with
a function-call syntax other than macros would be functions, object
construction and the operator(). None of those can give you a compile-time
constant.
[color=blue]
> As a side note, depending on the outcome of this thread, I may make a
> suggestion to comp.std.c++ to add the following functionality to the
> language, which might make use of the "switch" keyword:
>
> switch TwoPowerX( unsigned const x ) return 1U << x;
>
> int main()
> {
> int array[ TwoPowerX(5) ];
> }
>
>
> The "switch" keyword before the function definition would indicate that
> it can evaluate to a compile-time constant.[/color]

Abusing yet another keyword?

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.