Using invalid variable names in C++

Discussion in 'C++' started by Juha Nieminen, Mar 9, 2007.

  1. 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?
    Juha Nieminen, Mar 9, 2007
    #1
    1. Advertising

  2. Juha Nieminen

    Ian Collins Guest

    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.
    Ian Collins, Mar 9, 2007
    #2
    1. Advertising

  3. Juha Nieminen

    Rolf Magnus Guest

    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.
    Rolf Magnus, Mar 9, 2007
    #3
  4. Juha Nieminen

    Sarath Guest

    On Mar 9, 5:34 pm, 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?


    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?
    Sarath, Mar 9, 2007
    #4
  5. Juha Nieminen

    Lionel B Guest

    On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:

    > On Mar 9, 5:34 pm, 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?

    >
    > 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
    Lionel B, Mar 9, 2007
    #5
  6. Juha Nieminen

    Jim Langston Guest

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

    >>
    >> 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(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'
    Jim Langston, Mar 9, 2007
    #6
  7. Juha Nieminen

    Lionel B Guest

    On Fri, 09 Mar 2007 02:56:59 -0800, Jim Langston wrote:

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

    --
    Lionel B
    Lionel B, Mar 9, 2007
    #7
  8. Juha Nieminen

    Pete Becker Guest

    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)
    Pete Becker, Mar 9, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paddy McCarthy
    Replies:
    3
    Views:
    706
    Anthony J Bybell
    Sep 24, 2004
  2. Gerson Kurz
    Replies:
    4
    Views:
    378
    Larry Bates
    Jun 14, 2004
  3. wanwan
    Replies:
    3
    Views:
    427
    Alex Martelli
    Oct 14, 2005
  4. News123
    Replies:
    2
    Views:
    463
    John Machin
    Nov 26, 2008
  5. Peter Buckley
    Replies:
    5
    Views:
    197
    matt neuburg
    Feb 27, 2009
Loading...

Share This Page