Using invalid variable names in C++

J

Juha Nieminen

Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...
};

#ifdef __cplusplus
}
#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?
 
I

Ian Collins

Juha said:
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...
};

#ifdef __cplusplus
}
#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?

No, you are out of luck.
 
R

Rolf Magnus

Juha said:
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...
};

#ifdef __cplusplus
}
#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?

It's not 100% legal according to the C++ rules, but I've never seen a
compiler that wouldn't accept it:

#define class klass
#include "yourheaderfile.h"
#undef class

Then use the member under the name 'klass' in your C++ code.
 
S

Sarath

Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...

};

#ifdef __cplusplus}

#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?

Actually I could not find anything strange in this code.
extern "C" will process the definition inside as it process under C.
So the name mangling and other features will not be allowed/enbaled
for those definition. Seems there's no problem in using that
structure. I'm not sure whether I interpreted your questoin properly.
Could you please let us know if any error occurs while using this
structure?
 
L

Lionel B

Actually I could not find anything strange in this code.
extern "C" will process the definition inside as it process under C.
So the name mangling and other features will not be allowed/enbaled
for those definition. Seems there's no problem in using that
structure. I'm not sure whether I interpreted your questoin properly.
Could you please let us know if any error occurs while using this
structure?

The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.
 
J

Jim Langston

Lionel B said:
The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.

VC .net 2003

extern "C"
{
struct Whatever
{
int class;
};
};

console5.cpp(8) : error C2332: 'class' : missing tag name
console5.cpp(8) : error C2236: unexpected 'class' 'Whatever::__unnamed'
console5.cpp(8) : error C2467: illegal declaration of anonymous 'class'
console5.cpp(8) : error C2027: use of undefined type 'Whatever::__unnamed'
console5.cpp(8) : see declaration of 'Whatever::__unnamed'
 
L

Lionel B

VC .net 2003

extern "C"
{
struct Whatever
{
int class;
};
};

console5.cpp(8) : error C2332: 'class' : missing tag name
console5.cpp(8) : error C2236: unexpected 'class' 'Whatever::__unnamed'
console5.cpp(8) : error C2467: illegal declaration of anonymous 'class'
console5.cpp(8) : error C2027: use of undefined type 'Whatever::__unnamed'
console5.cpp(8) : see declaration of 'Whatever::__unnamed'

Yup...

#define class klass
extern "C"
{
struct Whatever
{
int class;
};
}
#undef class

int main()
{
Whatever obj;
obj.klass = 42; // ok
}
 
P

Pete Becker

Lionel said:
The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.

The problem is more fundamental: the keyword "class" is used in the
header in a place where it isn't valid, so it is an error. It doesn't
matter whether some other code tries to reference it.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top