N
nroberts
I've been playing around with an object library and have run into an
inconsistency between using it under a C compiler and using it under a
C++ compiler.
I wanted a way to declare "objects" statically, meaning they exist as
constants. I did something like the following:
struct Object {
struct Class const* isa;
};
struct String_ {
struct Object const _;
char const* buf;
int len;
};
extern void* __X_STR;
#define xstr(X) &(struct String_){ { __X_STR }, X, sizeof(X) };
The '__X_STR' variable is defined statically in a C file.
Later I can do the following:
void* s = xstr("Hello World!);
This works fine under gcc but pitches a fit under g++ about taking the
address of a temporary.
In C++ the expression (struct String_)... is in fact a temporary. I
expected, but do not know, that in C this would be an object in static
memory. Thus in C++ I'm doing something illegal but in C maybe it's
perfectly fine.
Is this correct?
inconsistency between using it under a C compiler and using it under a
C++ compiler.
I wanted a way to declare "objects" statically, meaning they exist as
constants. I did something like the following:
struct Object {
struct Class const* isa;
};
struct String_ {
struct Object const _;
char const* buf;
int len;
};
extern void* __X_STR;
#define xstr(X) &(struct String_){ { __X_STR }, X, sizeof(X) };
The '__X_STR' variable is defined statically in a C file.
Later I can do the following:
void* s = xstr("Hello World!);
This works fine under gcc but pitches a fit under g++ about taking the
address of a temporary.
In C++ the expression (struct String_)... is in fact a temporary. I
expected, but do not know, that in C this would be an object in static
memory. Thus in C++ I'm doing something illegal but in C maybe it's
perfectly fine.
Is this correct?