MS compiler bug?

M

Martijn

Hi,

The following code-fragment won't compile in the .Net-studio but it does
compile in Borland. So is anyone familiar with this problem, or someone
knows how to solve it?

void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
float derivative_state[dim];
...
}

The reported error is: C2057 - 'expected constant expression'. So the
compiler doesnt see 'dim' as an constant or something like that?!?

Anyone?
 
J

John Harrison

Martijn said:
Hi,

The following code-fragment won't compile in the .Net-studio but it does
compile in Borland. So is anyone familiar with this problem, or someone
knows how to solve it?

void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
float derivative_state[dim];
...
}

The reported error is: C2057 - 'expected constant expression'. So the
compiler doesnt see 'dim' as an constant or something like that?!?

Anyone?

No .NET is right. In this case constant expression means compile time
constant, which obviously dim isn't even though you've declared it as const.

The best way to do this is to use a vector.

#include <vector>

void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
std::vector<float> derivative_state(dim);
...

Now I can't see the rest of your code, but I wouldn't be surprised if it
compiled unchanged, even though you've switched from an array to a vector.

john
 
I

Ioannis Vranos

Martijn said:
Hi,

The following code-fragment won't compile in the .Net-studio but it does
compile in Borland. So is anyone familiar with this problem, or someone
knows how to solve it?

void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
float derivative_state[dim];
...
}

The reported error is: C2057 - 'expected constant expression'. So the
compiler doesnt see 'dim' as an constant or something like that?!?


It is not a constant expression, since it is not assigned a value at the
place of definition. It is as if you define a variabled-sized built in
array.

If you define dim inside the function scope then you will have no problem.
E.g.:



void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
const unsigned SIZE=7;

float derivative_state[SIZE];
...
}






Ioannis Vranos
 
C

Chris Theis

Martijn said:
Hi,

The following code-fragment won't compile in the .Net-studio but it does
compile in Borland. So is anyone familiar with this problem, or someone
knows how to solve it?

void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
float derivative_state[dim];
...
}

The reported error is: C2057 - 'expected constant expression'. So the
compiler doesnt see 'dim' as an constant or something like that?!?

Anyone?

The reported error is correct because dim is not a constant as the compiler
cannot see its actual value (which should be a defined constant). However,
it´s hard to believe that Borland will compile this without at least a
warning. Try to increase your warning levels and recompile under Borland
(BTW which version?).

HTH
Chris
 
A

Andre Kostur

Martijn said:
Hi,

The following code-fragment won't compile in the .Net-studio but it
does compile in Borland. So is anyone familiar with this problem, or
someone knows how to solve it?

void MidpointSolver::Solve(float* next_state, const float*
current_state, float time, float h, const unsigned int dim)
{
float derivative_state[dim];
...
}

The reported error is: C2057 - 'expected constant expression'. So the
compiler doesnt see 'dim' as an constant or something like that?!?

Anyone?

The reported error is correct because dim is not a constant as the
compiler cannot see its actual value (which should be a defined
constant). However, it´s hard to believe that Borland will compile
this without at least a warning. Try to increase your warning levels
and recompile under Borland (BTW which version?).

It is possible that Borland might have implemented variable length arrays
as a compiler extension? But from a C++ standpoint, dim isn't a compile-
time constant and cannot be used as an array size specifier (from a
Standard point of view....)
 
C

Chris Theis

[SNIP]> > The reported error is correct because dim is not a constant as the
It is possible that Borland might have implemented variable length arrays
as a compiler extension?
[SNIP]
I´ve also had that idea but I doubt that they do not at least give a hint
via a warning. Unfortunately I do not have a Borland compiler available to
check this. Hence, probably somebody else could shed some more light onto
this matter.

Chris
 
I

Ioannis Vranos

Chris Theis said:
Martijn said:
Hi,

The following code-fragment won't compile in the .Net-studio but it does
compile in Borland. So is anyone familiar with this problem, or someone
knows how to solve it?

void MidpointSolver::Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
float derivative_state[dim];
...
}

The reported error is: C2057 - 'expected constant expression'. So the
compiler doesnt see 'dim' as an constant or something like that?!?

Anyone?

The reported error is correct because dim is not a constant as the compiler
cannot see its actual value (which should be a defined constant). However,
it´s hard to believe that Borland will compile this without at least a
warning. Try to increase your warning levels and recompile under Borland
(BTW which version?).


Many compilers support VLAs as a system extension, the same happens with
long long. All of course give an error when in strict ISO mode.






Ioannis Vranos
 
R

Richard Herring

Chris Theis said:
[SNIP]> > The reported error is correct because dim is not a constant as the
It is possible that Borland might have implemented variable length arrays
as a compiler extension?
[SNIP]
I´ve also had that idea but I doubt that they do not at least give a hint
via a warning. Unfortunately I do not have a Borland compiler available to
check this. Hence, probably somebody else could shed some more light onto
this matter.
Not BCB5, at least:

void Solve(float* next_state, const float* current_state,
float time, float h, const unsigned int dim)
{
float derivative_state[dim];

}

[C++ Error] Unit1.cpp(6): E2313 Constant expression required
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top