Was sagt der Standard zu folgenden typedefs?



  • typedef int Foo;
    struct Bar {
       Foo foo;
       typedef long Foo;
    };
    

    Es gibt Compiler, die das einfach übersetzen und welche, die die sagen, dass das untere typedef nicht gestattet sei.



  • wuesste jetzt nicht was daran verboten sein sollte...

    weiss aber nicht, was der standard dazu sagt. wenn es nicht legal sein sollte, wuerde mich der grund aber brennend interessieren.


  • Mod

    das eine ist Foo das andere Bar::Foo - warum sollte das illegal sein?
    classes are namespaces.



  • Ich fänd es aber auch komisch.
    Der Comeau findet es in Ordnung, was mich vermuten lässte, das es in Ordnugn ist. Die gcc (3.3.x) will es aber z.B. nicht.


  • Mod

    3.4.1.7 Unqualified name lookup

    A name used in the definition of a class X outside of a member function body or nested class definition shall be declared in one of the following ways:

    - before its use in class X or be a member of a base class of X(10.2), or
    - if X is a nested class of class Y(9.7), before the definition of X in Y, or shall be a member of a base class of Y (this lookup applies in turn to Y's enclosing classes, starting with the innermose enclosing class), or
    - if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or
    - if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the definition of class X in namespace N or in one of N's enclosing namespaces.

    3.3.6.1 Class scope

    The following rules describe the scope of names declared in classes.

    1. The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).

    2. A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for violation of this rule.

    3. If reordering member declarations in a class yields an alternate valid program under (1) and (2), the program is ill-formed, no diagnostic is required.
      etc.

    wenn ich es richtig verstehe, verstösst dein beispiel gegen regel 3.3.6.1 2)

    konsequenterweise geht das hier geht mit gcc(3.4.2) auch nicht:

    typedef int Foo;
    struct Bar {
       Bar::Foo foo;
       typedef long Foo;
    };
    

    das hier dagegen funktioniert, was wohl der grund für die konfusion ist:

    typedef int Foo;
    struct Bar {
        Bar()
        {
            Foo i = 10;
        }
       typedef long Foo;
    };
    

Anmelden zum Antworten