[cfe-dev] C++ typedef merging

James Widman widman at gimpel.com
Sun Dec 2 15:57:19 PST 2007

On Dec 2, 2007, at 1:42 PM, Chris Lattner wrote:

> There is also the
> strangeness in C++ where you have:
> struct stat { .. };
> struct stat stat();
> "stat" is a function, but "struct stat" is the type,

Yep, according to 3.4.4 basic.lookup.elab.

> even though the
> tag namespace is normally searched for normal identifiers.

Note, in ISO C++ there is no concept of "tag names", "ordinary names",  
"tag name space" or "ordinary name space" as in ISO C.

In the pre-Standard days, C++ did not even allow the struct stat case,  
but eventually the following rule from 3.3.7 basic.scope.hiding was  
added (because losing compatibility with POSIX (and probably other  
libraries) would have been a deal-breaker):

         A class name (9.1) or enumeration name (7.2) can be
         hidden by the name of an object, function, or enumerator
         declared in the same scope.  If a class or enumeration
         name and an object, function, or enumerator are declared
         in the same scope (in any order) with the same name, the
         class or enumeration name is hidden wherever the object,
         function, or enumerator name is visible.

Note that this rule allows just enough hiding to maintain  
compatibility with this kind of C code while rendering the following  
examples ill-formed:

struct A {};
namespace A{}                  // Error

int B;
namespace B{}                  // Error

int C();
template <class T> struct C{}; // Error

template< class T> int D();
struct D{};                    // Error

James Widman
Gimpel Software

More information about the cfe-dev mailing list