# c++ constant expressions

## Recommended Posts

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?

##### Share on other sites
foo will always equal whatever bar was when the expression was first executed.

Edit: Removed nonsensical source example [grin]

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by IccarusI 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;...

##### Share on other sites
Quote:
 Original post by IccarusIs 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++.

##### Share on other sites
Just get a different compiler. Mine allows variable length arrays.

##### Share on other sites
Quote:
 Original post by Anonymous PosterJust 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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by Puzzler183Anyway, 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).

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978763

• 21
• 14
• 12
• 41