Known at compile-time

J

JKop

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
 
P

Peter van Merkerk

JKop said:
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).
 
J

John Harrison

JKop said:
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
 
J

JKop

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
 
T

tom_usenet

John Harrison posted:



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
 
R

Richard Herring

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...
[/QUOTE]
Why this suicidal urge to produce guaranteed non-portable code?
 
J

JKop

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
 
J

John Harrison

JKop said:
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
 
P

Peter van Merkerk

JKop said:
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.
 
T

tom_usenet

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
 
J

JKop

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

-JKop
 
R

Richard Herring

In message <[email protected]>, JKop <[email protected]>
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top