Wednesday, May 13, 2009

typedef in C and C++

I like C++, but it is a complex language. After programming in it for 19 years, you'd think I would know it pretty well, but I'm still learning things even now.

Recently, I discovered that typedef works differently in C++ than in C. In C, you can't repeat a typedef even if it's exactly the same as the original definition. For example:

typedef int T;
typedef int T;

will give you a compiler error in C. However, this is perfectly legal in C++. I'm pretty sure this wasn't the case in early C++. I wonder when this changed? Anybody know?

I found a very good web site for the differences in C and C++ here:


gorshing said...

I don't have access to any kind of older C/C++ compiler, what about watcom? I thought you had a version of this which was a few years old?

BTW - What is your definition of 'early' as in "I'm pretty sure this wasn't the case in early C++"?

Paul Carter said...

I little googling found this patch to gcc in 1999: it appears duplicate typedefs were allowed in the 1998 C++ standard.

By early I was thinking of the early 90's when I first started with C++.