On Wed, 24 Jun 2009 17:04:15 +0200, Michael Widenius <monty@stripped>
wrote:
>
> Hi!
>
>>>>>> "Mats" == Mats Kindahl <Mats.Kindahl@stripped> writes:
>
> Mats> Tor Didriksen wrote:
>>> hi
>>>
>>> "Structure types are typedef'ed to an all-upper-case identifier."
>>> Can someone explain the reasoning behind this?
>>> ALL_UPPERCASE_NAME says DANGEROUS_THIS_IS_A_MACRO to me, rather than
>>> struct/class.
>>>
>>> typedef struct foo { ... } FOO;
>>> Is legacy C-style, and does not belong in a C++ style guide (imho).
>
> Mats> I agree.
>
> Mats> In addition, it is a problem because there cannot be multiple
> typedefs, but
> Mats> there can be multiple forward declarations, which makes it very
> difficult to do
> Mats> a forward declaration of a structure using, e.g.:
>
> Mats> typedef struct foo FOO;
>
> Mats> In the event that the header file is included by some other file
> that does a
> Mats> similar forward declaration, the compiler will throw an error.
>
> We have already solved this in many places of MySQL / mysys by using
> struct
> foo for cases where the typedef is not yet done so I wouldn't call
> this a notable problem.
>
> I also tested this with gcc and didn't get any error for the
> following file:
>
> typedef struct foo FOO;
> struct foo {int a;};
> typedef struct foo FOO;
>
> struct foo a;
> FOO b;
>
> What is it that would give an error?
You cannot use FOO in a forward declaration.
>
> Mats> I would prefer if we never used typedef to introduce aliases for
> Mats> struct/class/union, they cause more problem that it's worth, and
> they are not
> Mats> needed in C++ at all.
>
> MySQL was coded in part so that it would be able to easily mix C++ and
> C code and allow you to move things from C++ and C with little work.
>
> As MySQL is using C libraries with typedefs, you can't easily get rid
> of all the typedefs.
I was asking for a deprecation of the rule, not a removal of *all*
typedefs.
Having typedefs in C-land makes sense (you save a few characters by typing
'FOO' rather than 'struct foo'),
but not in C++ where 'foo' is handled as a type by the compiler.
Why do we want two different names for a thing, when one will do?
The typedef rule is also used very inconsistently in the codebase
(see Roy's earlier example)
>
> Regards,
> Monty
-- didrik