473,513 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Known at compile-time

The following compiles for me with G++:
const unsigned int z(5U);

unsigned int y(5 + 6 - 4 / 2 + 3 * 6 - z);

inline unsigned int x()
{
return y * 4 + z - 9 * 3;
}

inline unsigned int w()
{
unsigned int temp( ( x() > 8 ? x() + 5 : x() - 4 ) );

temp -= 4;

temp *= 5;

return temp + 6;
}

unsigned int v(unsigned int input)
{
if ( input > 16 )
{
return input + w();
}
else
{
return input - x();
}
}

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char a[z];

y += 4;

char b[y];

char c[x()];

char d[w()];

char e[v(w())];

cout << sizeof(a) << endl
<< sizeof(b) << endl
<< sizeof(c) << endl
<< sizeof(d) << endl
<< sizeof(e);
}
I'm wondering if this is guaranteed to compile on every C++ Standard
compliant compiler. The values are indeed "knowable" at compile time, but it
seems to me that a compiler has a bit of work to do.
It prints the following for me:

5
26
82
421
842

Anyone else want to give it a try?
-JKop
Jul 22 '05 #1
13 1691

"JKop" <NU**@NULL.NULL> wrote in message
news:6z*****************@news.indigo.ie...
The following compiles for me with G++:
const unsigned int z(5U);

unsigned int y(5 + 6 - 4 / 2 + 3 * 6 - z);

inline unsigned int x()
{
return y * 4 + z - 9 * 3;
}

inline unsigned int w()
{
unsigned int temp( ( x() > 8 ? x() + 5 : x() - 4 ) );

temp -= 4;

temp *= 5;

return temp + 6;
}

unsigned int v(unsigned int input)
{
if ( input > 16 )
{
return input + w();
}
else
{
return input - x();
}
}

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char a[z];

y += 4;

char b[y];

char c[x()];

char d[w()];

char e[v(w())];

cout << sizeof(a) << endl
<< sizeof(b) << endl
<< sizeof(c) << endl
<< sizeof(d) << endl
<< sizeof(e);
}
I'm wondering if this is guaranteed to compile on every C++ Standard
compliant compiler.
No
The values are indeed "knowable" at compile time, but it
seems to me that a compiler has a bit of work to do.
It prints the following for me:


g++ has an extension where array bounds do NOT have to constant. Try
compiling with the -ansi switch.

I forget what the rules for compile time constants are exactly, but it does
not include function calls. Simple integral expressions and sizeof are OK,
pointer arithmetic is probably OK as well.

john
Jul 22 '05 #2
JKop wrote:
The following compiles for me with G++:
const unsigned int z(5U);

unsigned int y(5 + 6 - 4 / 2 + 3 * 6 - z);

inline unsigned int x()
{
return y * 4 + z - 9 * 3;
}

inline unsigned int w()
{
unsigned int temp( ( x() > 8 ? x() + 5 : x() - 4 ) );

temp -= 4;

temp *= 5;

return temp + 6;
}

unsigned int v(unsigned int input)
{
if ( input > 16 )
{
return input + w();
}
else
{
return input - x();
}
}

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char a[z];

y += 4;

char b[y];

char c[x()];

char d[w()];

char e[v(w())];

cout << sizeof(a) << endl
<< sizeof(b) << endl
<< sizeof(c) << endl
<< sizeof(d) << endl
<< sizeof(e);
}
I'm wondering if this is guaranteed to compile on every C++ Standard
compliant compiler. The values are indeed "knowable" at compile time,
But that doesn't make it a compile time constant.
but it seems to me that a compiler has a bit of work to do.
It prints the following for me:

5
26
82
421
842

Anyone else want to give it a try?


MSVC6.0 chokes on it.

You can get away with it on G++ because it supports variable length
arrays. In other other when you do:

char b[y];

y doesn't have to be a compile time constant.

The code below will probably compile on G++ as well (I haven't got G++
installed on the system I'm using now so I cannot test it myself), even
though the compiler has no way of knowing in advance what y will be.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int y;
std::cin >> y;
char b[y];
cout << sizeof(b) << endl;

return 0;
}
However variable length arrays are not standard C++ (but it standard C99).

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #3
John Harrison posted:
g++ has an extension where array bounds do NOT have to constant. Try compiling with the -ansi switch.

It still compiles with -ansi.
Off to do more experiments...
-JKop
Jul 22 '05 #4
On Tue, 20 Jul 2004 13:11:37 GMT, JKop <NU**@NULL.NULL> wrote:
John Harrison posted:
g++ has an extension where array bounds do NOT have to

constant. Try
compiling with the -ansi switch.

It still compiles with -ansi.
Off to do more experiments...


Try -ansi -pedantic.

GCC has far too many extensions switched on by default for its own
good.

Tom
Jul 22 '05 #5
In message <dS*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
John Harrison posted:
g++ has an extension where array bounds do NOT have to

constant. Try
compiling with the -ansi switch.

It still compiles with -ansi.
Off to do more experiments...

Why this suicidal urge to produce guaranteed non-portable code?

--
Richard Herring
Jul 22 '05 #6

Why the hell is g++ so famous? I heard everyone talking
about it and so assumed it was the best Standard compliant
compiler out there.

I'm running WindowsXP, can anyone suggest a Standard-
compliant, good optimizing compiler?
-JKop
Jul 22 '05 #7

"JKop" <NU**@NULL.NULL> wrote in message
news:jQ*****************@news.indigo.ie...

Why the hell is g++ so famous? I heard everyone talking
about it and so assumed it was the best Standard compliant
compiler out there.

I'm running WindowsXP, can anyone suggest a Standard-
compliant, good optimizing compiler?


VC++ 7.1, its downloadable free from MS website somewhere.

It's very standards compliant, and (so I've heard) much better at optimising
than g++. I think MS have a bad reputation in this group because of VC++ 6,
which was a long way from standard compliance.

john
Jul 22 '05 #8
JKop wrote:
Why the hell is g++ so famous? I heard everyone talking
about it and so assumed it was the best Standard compliant
compiler out there.
No, it isn't. But as far as language support is concerned it is very
good, it is free and it is available on many platforms. The fact that a
compiler supports many non-standard extensions does not say anything
about its standards compliancy.
I'm running WindowsXP, can anyone suggest a Standard-
compliant, good optimizing compiler?


Comeau is probably the most conformant C++ compiler front-end.
http://www.comeaucomputing.com/

On this website you can also test some code snippets online
(http://www.comeaucomputing.com/tryitout/).

The Comeau compiler is just a front-end, you still need a compiler that
turns the output of this front-end in to something executable. It is
based on the EDG front-end, which I believe is also used by the Intel
compiler. The Intel compiler is well known for its optimizer, so
probably this is what you are looking for.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #9
On Tue, 20 Jul 2004 14:17:51 GMT, JKop <NU**@NULL.NULL> wrote:

Why the hell is g++ so famous? I heard everyone talking
about it and so assumed it was the best Standard compliant
compiler out there.

I'm running WindowsXP, can anyone suggest a Standard-
compliant, good optimizing compiler?


G++ is very standard compliant in version 3.4+, but only if you add
-ansi -pedantic to the command line.

On XP, I'd possibly suggest Intel C++, which uses EDG's front end and
is therefore even more compliant than GCC, and also employs whole
program optimization and advanced knowledge of Intel's chips to give
good performance. But G++ has the advantage of being free, whilst
being almost as good.

Tom
Jul 22 '05 #10
Richard Herring posted:
In message <dS*****************@news.indigo.ie>, JKop <NU**@NULL.NULL> writes
John Harrison posted:
g++ has an extension where array bounds do NOT have to constant. Try compiling with the -ansi switch.

It still compiles with -ansi.
Off to do more experiments...

Why this suicidal urge to produce guaranteed non-portable

code?

faggot.
-JKop
Jul 22 '05 #11
In message <r9*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Richard Herring posted:
In message <dS*****************@news.indigo.ie>, JKop

<NU**@NULL.NULL>
writes
John Harrison posted:

g++ has an extension where array bounds do NOT have toconstant. Try compiling with the -ansi switch.
It still compiles with -ansi.
Off to do more experiments...

Why this suicidal urge to produce guaranteed non-portable

code?

faggot.
-JKop


Declaration or definition?

--
Richard Herring
Jul 22 '05 #12

Whether you are consciously aware of it or not, a lot of your posts tend no
other purpose than to irritate people.

-JKop
Jul 22 '05 #13
In message <ER*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes

[please quote some context, and make it clear who's being addressed. Not
every reader has the full context of the tread available to them]

Whether you are consciously aware of it or not, a lot of your posts tend no
other purpose than to irritate people.

PKB.

If my posts irritate people who post inaccurate information or
ill-informed opinion disguised as fact, they serve a useful purpose.

8.3.4 says the dimension of an array must be an integral
constant-expression greater than zero.

5.19 says (inter alia) that in integral constant-expressions,
assignment, increment, decrement, function-call and comma operators
cannot be used.
HTH.
HAND.
--
Richard Herring
Jul 22 '05 #14

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

Similar topics

5
7207
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
0
1451
by: Anurag | last post by:
Hi, I understand that the dbm cfg parameter "DISCOVER" controls whether Search or Known discovery is applicable. Additionally, Search discovery provides a superset of Known discovery. I completely...
4
1232
by: CJ Taylor | last post by:
Alright, So this morning (as we are a week from launch on our product) I come across an error as I add a simple thing to our system. A display column in our dataset for databinding for display...
11
407
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
5
28867
by: walsht | last post by:
When I compile the code snipets below I'm producing the error: storage size of 'frm' isn't known. dll.h : /* * File: dll.cpp * Defines the data link layer's interface. */ #ifndef __FRAME_
9
1917
by: vbfoobar | last post by:
Hello I am looking for python code that takes as input a list of strings (most similar, but not necessarily, and rather short: say not longer than 50 chars) and that computes and outputs the...
3
11898
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I get a strange compile error when compile such simple program. Any ideas? foo.c: In function `main': foo.c:5: error: storage size of 'var' isn't known foo.c
4
1720
by: Terence Wilson | last post by:
I'm having trouble designing a container for all the basic types(char, int, float, double). The container should be able to hold contiguous arrays of type T. I would like some kind of generic...
5
4019
by: Jameson | last post by:
Hi, I have a list of known colours, generated using: Dim colorNames As New System.Collections.Generic.List(Of String) For Each known As KnownColor In .GetValues(GetType(KnownColor)) Dim...
3
15741
by: =?Utf-8?B?Sm9uIEU=?= | last post by:
I have an interface class with maybe eight functions, defined in one workspace and am defining a class in a second workspace that derives from this interface. Unfortunately only 7 of the 8...
0
7160
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
7537
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...
1
7099
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
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3233
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
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 ...
1
799
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.