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?
c++ constant expressions
Is there anyway to make a constant integer out of a variable passed.
I've just tried
foo will always equal whatever bar was when the expression was first executed.
Edit: Removed nonsensical source example [grin]
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
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
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
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 useto use a constant expression.const int foo = bar;
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 triedbut 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?const int foo = bar;
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++.
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.
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement