Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using invalid variable names in C++

Reply
Thread Tools

Using invalid variable names in C++

 
 
Juha Nieminen
Guest
Posts: n/a
 
      03-09-2007
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-09-2007
Juha Nieminen wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-09-2007
Juha Nieminen wrote:

> 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.

 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      03-09-2007
On Mar 9, 5:34 pm, Juha Nieminen <(E-Mail Removed)> wrote:
> 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?

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      03-09-2007
On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:

> On Mar 9, 5:34 pm, Juha Nieminen <(E-Mail Removed)> wrote:
>> 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?


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.

--
Lionel B
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-09-2007
"Lionel B" <(E-Mail Removed)> wrote in message
news:esre5h$68m$(E-Mail Removed)...
> On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:
>
>> On Mar 9, 5:34 pm, Juha Nieminen <(E-Mail Removed)> wrote:
>>> 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?

>
> 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.
>
> --
> Lionel B


VC .net 2003

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

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


 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      03-09-2007
On Fri, 09 Mar 2007 02:56:59 -0800, Jim Langston wrote:

> "Lionel B" <(E-Mail Removed)> wrote in message
> news:esre5h$68m$(E-Mail Removed)...
>> On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:
>>
>>> On Mar 9, 5:34 pm, Juha Nieminen <(E-Mail Removed)> wrote:
>>>> 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?

>>
>> 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.
>>
>> --
>> Lionel B

>
> VC .net 2003
>
> extern "C"
> {
> struct Whatever
> {
> int class;
> };
> };
>
> console5.cpp( : error C2332: 'class' : missing tag name
> console5.cpp( : error C2236: unexpected 'class' 'Whatever::__unnamed'
> console5.cpp( : error C2467: illegal declaration of anonymous 'class'
> console5.cpp( : error C2027: use of undefined type 'Whatever::__unnamed'
> console5.cpp( : 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
}

--
Lionel B
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-09-2007
Lionel B wrote:
>
> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic variable names or using a variable as another's name Peter Buckley Ruby 5 02-27-2009 06:05 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
Invalid URI: There is an invalid sequence in the string. Error!! Patrick.O.Ige ASP .Net 1 07-02-2006 11:21 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
setattr using invalid attribute names - bug or feature? Gerson Kurz Python 4 06-14-2004 07:55 PM



Advertisments