Re: Style convention question



On Fri, Mar 09, 2001 at 12:39:41PM -0500, Owen Taylor wrote:
> The reason is that sometimes you need forward declarations:
>  typedef struct _A A;
>  typedef struct _B B;
>  struct _A {
>    B *b;
>  };
>  struct _B {
>    A *a;
>  };

This should be in the form:

    #ifdef __cplusplus
    #  define DECLARE_STRUCT(A)    struct A
    #else
    #  define DECLARE_STRUCT(A)    typedef struct A A
    #endif

    DECLARE_STRUCT(A);
    DECLARE_STRUCT(B);

    struct A
    {
       ...
    };

    struct B
    {
       ...
    };

But traditionally, this has always been:

    typedef struct _A A;
    typedef struct _B B;

    struct _A
    {
       ...
    };

    struct _B
    {
       ...
    };

The second is far more common (and used in gtk/glib/etc.), but is primarily
a result of people not understanding that "struct A" is in a different name
space than "typedef ... A". This misunderstanding is then propagated.

It's not "bad", other than the fact that it looks hacky, and it requires
more symbols to be allocated in a C++ compiler, which while holding "struct A"
in a different namespace than "typedef ... A", will search both namespaces
when determining which "type" an unknown keyword might be referencing. Giving
a preference to the "typedef". For the situation above, they reference one
and the same, and the additional logic/memory is wasted.

mark

-- 
mark mielke cc/markm ncf ca/markm nortelnetworks com __________________________
.  .  _  ._  . .   .__    .  . ._. .__ .   . . .__  | Neighbourhood Coder
|\/| |_| |_| |/    |_     |\/|  |  |_  |   |/  |_   | 
|  | | | | \ | \   |__ .  |  | .|. |__ |__ | \ |__  | Ottawa, Ontario, Canada

  One ring to rule them all, one ring to find them, one ring to bring them all
                       and in the darkness bind them...

                           http://mark.mielke.cc/





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]