From: Mats Kindahl Date: June 25 2009 9:12am Subject: Re: MySql coding style: Request for deprecation of UPPERCASE typedefs List-Archive: http://lists.mysql.com/internals/36995 Message-Id: <4A433F73.6010002@sun.com> MIME-Version: 1.0 Content-Type: text/plain; CHARSET=US-ASCII Content-Transfer-Encoding: 7BIT Michael Widenius wrote: > Hi! > >>>>>> "Mats" == Mats Kindahl 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? Sorry, I confused two situations: this will not produce an error. The real problem is the following: In some places of the code, it is "typedef struct st_foo FOO" and in some places it is "struct FOO" and you always have to check which one it is when forward declaring the name, which is wasting time (I'm sitting with this right now, and it is not always as easy as hitting M-.). - If it is "typedef struct st_foo FOO", "struct FOO" will throw an error. - If it is "struct FOO", then "typedef struct st_foo FOO" will throw an error. > 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. I think that in practice it is not possible to move from C++ to C (since C++ code inevitable will contain C++ constructs), and that the extra work required to move from C to C++ does not really motivate using typedefs. > As MySQL is using C libraries with typedefs, you can't easily get rid > of all the typedefs. It depends on your definition of easy, but I think I can with only minor changes to the C code... Just my few cents, Mats Kindahl -- Mats Kindahl Senior Software Engineer Database Technology Group Sun Microsystems