home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,426 of 33,346   
   Johannes Schaub to All   
   Re: workaround to C "typedef struct name   
   27 Aug 11 18:04:18   
   
   From: schaub.johannes@googlemail.com   
      
   Daniel Krügler wrote:   
      
   > Am 25.08.2011 14:30, schrieb Brian L:   
   ...   
   > struct C {   
   >    typedef struct name *name;   
   > };   
   >   
   > This works, because of 7.1.3 [dcl.typedef] p4:   
   >   
   > "In a given class scope, a typedef specifier can be used to redefine any   
   > class-name declared in that scope that is not also a typedef-name to   
   > refer to the type to which it already refers."   
   >   
      
   Well, it would work independently of 7.1.3. You are not redefining a class-   
   name declared in the scope of class "C". The class-name is declared   
   (invisibly, c.f. note at 3.3p4) in the namespace scope surrounding class C   
   (see 3.3.2p6 second bullet). The class is a member of that namespace.   
      
   And you are not referring to the same type both times.   
      
   > Within C the first part 'struct name' refers to a yet undeclared class   
   > type name in the global scope and so we define C::name as a pointer to   
   > ::name. But this means that always need to refer to C::name instead of   
   > name as in your OP.   
   >   
      
   This is correct, but I wonder whether this is not a violation of the name-   
   must-have-same-meaning rule. Because the use of "name" in "struct name"   
   refers to the class name in the surrounding namespace, but if you would use   
   "struct name" again in the class with the complete scope of "C" available,   
   it would not anymore refer to that namespace member name, but would   
   refer to   
   the typedef-name. That would make the program ill-formed (by 7.1.6.3p2),   
   but   
   3.3.7p1.2 is violated regardless.   
      
   For a pedantic interpretation of 3.3.7.p1.2 and 7.1.3, we can argue even   
   the   
   following runs afoul of this rule too:   
      
      struct A {   
        struct B { };   
        typedef struct B B;   
      };   
      
   That's because the model by which the typedef name is introduced inspite of   
   the already existing class name is redefinition of the name. Not   
   overloading/hiding and not different identifier namespaces like in C. So in   
   the complete scope of "A", a use like "struct B" can not refer to the   
   declaration of the class-name introduced by "struct B { };", because that   
   name was redefined to refer to the typedef-name introduced by the typedef-   
   declaration. Note that in this case, the program would not be affected by   
   7.1.6.3p2, because of the rule added to the FDIS at 7.1.3p5. But 3.3.7p1.2   
   will draw it ill-formed; NDR. But I believe the intention is for this   
   example code to be valid, though.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca