473,408 Members | 1,951 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,408 software developers and data experts.

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
Jun 25 '06 #1
2 1857
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
1
by: Mario T. Lanza | last post by:
I am working with Visual Studio. The solution I am developing is composed of about 8 separate projects. Some of these projects represent different tiers in the N-tiered architecture (data,...
4
by: frankg | last post by:
When I compile this file ------------------------------------------------------ #include <stdarg.h> template <class Any> void var_arg_func(int dimension_count, ...) { int dimensions;...
0
by: Jordan Willms | last post by:
My xsl stylesheet is as simple as follows: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2"...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
1
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows...
3
by: NvrBst | last post by:
Right now I have C99 code in .c extensions. I compile it in VSC++ and it complains about a lot of errors. I change the extensions to .cpp and compile in VSC++ and it succeeds. Is there a way...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
6
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.