By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Brainstorm for compile-time constants

P: n/a

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
Jun 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <Xn***************************@194.125.133.14>,
Frederick Gotham <fg*******@SPAM.com> wrote:
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) ];
}


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
Jun 25 '06 #2

P: n/a
Frederick Gotham wrote:

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!)
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.
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.


Abusing yet another keyword?

Jun 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.