473,587 Members | 2,547 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 1702

"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.merke rk(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.indi go.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.merke rk(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

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

Similar topics

5
7223
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 error) Save your work and restart Visual Studio .NET." has anyone seen this bug and can you confirm one way or the other whether or not it can corrupt...
0
1459
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 understand the scenario where SEARCH is applicable and where KNOWN is applicable. My question is very basic: >From a server's perspective, what...
4
1238
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 purposes only. So everything goes smoothly, compile, start running, everything looking good. then I go to update a row.. get a System.Data.Readonly...
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters,...
5
28878
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
1927
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 python regular expression that matches these string values (not necessarily strictly, perhaps the code is able to determine patterns, i.e. families...
3
11903
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
1724
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 iterator for accessing the data that is type independent in order that I could write generic algorithms. Basically I need something like std::vector...
5
4027
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 specific As Color = Color.FromKnownColor(known)
3
15748
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 functions in my derived class compile whilst just one of them refuses to be recognised, leading to an error message "...does not implement interface...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2358
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 we have to send another system
1
1454
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.