Sign in to follow this  
Iccarus

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 this post


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

Edit: Removed nonsensical source example [grin]

Share this post


Link to post
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 this post


Link to post
Share on other sites
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;
...


Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just get a different compiler. Mine allows variable length arrays.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this