mcn
May 29th, 2002, 06:43 AM
trying to avoid using of Macros for declarations of the form
char u[MAXLEN];
because many old code in our departement was written before gcc was able to handle namespaces correctly, we used classes with static data members/methods instead. now we tried to port the following code to be used with VC++ 6.0
// a.hh
class A {
private:
A() {}
virtual ~A() {}
public:
static const int a;
};
// a.cc
#include "a.h"
const int A::a = 7;
b.hh
class B {
public:
B();
virtual ~B() {}
};
// b.cc
#include "a.h"
#include "b.h"
B::B()
{
char u[A::a]; // error will occur here!!!
std::cerr << "A::a is " << A::a << std::endl;
std::cerr << "sizeof(u) is " << sizeof(u) << std::endl;
}
// test.cc
#include "b.h"
int main(int argc, char* argv[])
{
B b;
return 0;
}
things which works well with gcc yield the following the compiler errors using VC:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'u' : unknown size
error C2070: illegal sizeof operand
is this behaviour covered by the standard. i know that problems occur during dynamic initialization of A::a at runtime and its value may be undefined or 0 (statically initialized) but i never saw problems during compiletime.
mark
char u[MAXLEN];
because many old code in our departement was written before gcc was able to handle namespaces correctly, we used classes with static data members/methods instead. now we tried to port the following code to be used with VC++ 6.0
// a.hh
class A {
private:
A() {}
virtual ~A() {}
public:
static const int a;
};
// a.cc
#include "a.h"
const int A::a = 7;
b.hh
class B {
public:
B();
virtual ~B() {}
};
// b.cc
#include "a.h"
#include "b.h"
B::B()
{
char u[A::a]; // error will occur here!!!
std::cerr << "A::a is " << A::a << std::endl;
std::cerr << "sizeof(u) is " << sizeof(u) << std::endl;
}
// test.cc
#include "b.h"
int main(int argc, char* argv[])
{
B b;
return 0;
}
things which works well with gcc yield the following the compiler errors using VC:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'u' : unknown size
error C2070: illegal sizeof operand
is this behaviour covered by the standard. i know that problems occur during dynamic initialization of A::a at runtime and its value may be undefined or 0 (statically initialized) but i never saw problems during compiletime.
mark