c++ constant expressions

Started by
7 comments, last by joanusdmentia 18 years ago
Is there anyway to make a constant integer out of a variable passed. I've just tried
const int foo = bar;
but it doesn't work (that probably isn't surprising to you lot) is there anyway to make it so that foo is always equal to the bar passed to it?
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
Advertisement
foo will always equal whatever bar was when the expression was first executed.

Edit: Removed nonsensical source example [grin]
I want to use a variable to declare the size of an array (no I can't use vector) because the size will not be known until the variable is set.

Arrays can't take variables as their size declaration (well Visual Studio.NET 2003 says its an error if I do it) and the same when I use
const int foo = bar;
to use a constant expression.

I get:
(188) : error C2057: expected constant expression
(188) : error C2466: cannot allocate an array of constant size 0
(188) : error C2133: 'temp' : unknown size
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
Quote:Original post by Iccarus
I want to use a variable to declare the size of an array (no I can't use vector) because the size will not be known until the variable is set.

Arrays can't take variables as their size declaration (well Visual Studio.NET 2003 says its an error if I do it) and the same when I use
const int foo = bar;
to use a constant expression.

I get:
(188) : error C2057: expected constant expression
(188) : error C2466: cannot allocate an array of constant size 0
(188) : error C2133: 'temp' : unknown size



Allocate on the heap using new. If you need, say, an array to store 500 ints, you would do:

int* ints = new int[500];ints[0] = 100;ints[1] = 101;...
Quote:Original post by Iccarus
Is there anyway to make a constant integer out of a variable passed.

I've just tried
const int foo = bar;
but it doesn't work (that probably isn't surprising to you lot) is there anyway to make it so that foo is always equal to the bar passed to it?


So, what you're saying is that you want to define foo as bar permanently at runtime? Unfortunately, you can't do that in C or C++. In single assignment languages, you can, but not C or C++.
Just get a different compiler. Mine allows variable length arrays.
Quote:Original post by Anonymous Poster
Just get a different compiler. Mine allows variable length arrays.


I would recommend against that. That is a non-standard extension. You should avoid non-standard extensions wherever possible.
Actually, I'm pretty sure that stack allocated dyanmic arrays are standard in C99...

Anyway, when it says constant expression, it doesn't mean a constant variable. You can look at the standard at your liesure for the exact definition of a constant expression.

And as for allocating the arrays, use new.
Quote:Original post by Puzzler183
Anyway, when it says constant expression, it doesn't mean a constant variable. You can look at the standard at your liesure for the exact definition of a constant expression.


Exactly, a constant expression refers to something that the compiler can deduce at compile time (therefore it is constant during runtime).
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement