Mehrfachverwendung von Bezeichnern



  • In dieses Datentypen sollen Grundlage fuer JSON-Objekte sein. Wie zu sehen ist, werden Bezeichner wie array mehrfach verwendet, als Typ, Enum oder Member. Ich wuerde Compilerfehler erstmal erwarten, wobei es keine Mehrdeutigkeiten geben sollte. Leider uebersetzt es VS 2012 anstandslos, waehrend gcc 4.7.1 (ideone.com) Fehler anzeigt. Leider stehen mir grad keine weiteren Testcompiler zur Verfuegung. Die Frage: Ist folgendes gueltiges C++11?

    #include <map>
    #include <vector>
    #include <string>
    
    namespace json {
    
    class value;
    
    typedef std::vector<value> array;
    typedef std::map<std::string, value> object;
    typedef std::string string;
    
    enum class type
    {
        null,
        boolean,
        integer,
        number,
        string,
        object,
        array
    };
    
    class value
    {
    public:
        type type;
        union
        {
            int integer;
            double number;
            bool boolean;
            string* string;
            array* array;
        };
    };
    
    }
    

  • Mod

    knivil schrieb:

    Ist folgendes gueltiges C++11?

    #include <map>
    #include <vector>
    #include <string>
    
    namespace json {
    
    class value;
    
    typedef std::vector<value> array;
    typedef std::map<std::string, value> object;
    typedef std::string string;
    
    enum class type
    {
        null,
        boolean,
        integer,
        number,
        string,
        object,
        array
    };
    
    class value
    {
    public:
        type type;
        union
        {
            int integer;
            double number;
            bool boolean;
            string* string;
            array* array;
        };
    };
    
    }
    

    nein.

    n3337 schrieb:

    3.3.7 Class scope [basic.scope.class]
    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 point of declaration, but also of all function bodies, brace-or-equal-initializers of non-static
      data members, and default arguments 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 a 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.
    4. A name declared within a member function hides a declaration of the same name whose scope extends
      to or past the end of the member function’s class.
      ...


  • Besagte Zeile sagt, folgendes ist Mist

    type type; 
    bool boolean; 
    string* string; 
    array* array;
    

    Eigentlich klar ... aber was ist mit dem ersten Teil?

    typedef std::vector<value> array; 
    typedef std::map<std::string, value> object; 
    typedef std::string string; 
    
    enum class type 
    {
        ...
        string, 
        object, 
        array 
    };
    

    Es wird zumindest vom gcc 4.7.1 geschluckt.



  • Ginge es nicht so?

    class value
      {
      public:
        json::type type;
        union
        {
          int integer;
          double number;
          bool boolean;
          json::string* string;
          json::array* array;
        };
      };
    

    Damit dürfte doch im Scope von value alles eindeutig sein.


  • Mod

    knivil schrieb:

    was ist mit dem ersten Teil?

    typedef std::vector<value> array; 
    typedef std::map<std::string, value> object; 
    typedef std::string string; 
      
    enum class type 
    {
        ...
        string, 
        object, 
        array 
    };
    

    Es wird zumindest vom gcc 4.7.1 geschluckt.

    Verschiedene Scopes, also kein Problem.



  • @Furble Wurble: Ja, werde ich so machen. Ich war nur sehr verwirrt, warum ohne Warnung oder Fehler VS 2012 das uebersetzte.

    Dank auch dir camper.



  • Der MSVC ist bei vielen Dingen etwas toleranter und schreit den User nicht sofort an. Ist ganz angenehm, wenn man nur auf dem schreibt, aber sobald man Standardkonformen Code scheiben will und den Compiler auch mal wechselt, muss man öfters mal aufpassen...



  • Toleranter nicht, aber genauer. Wenn eine Typebezeichnung erwartet wird, dann sucht er auch nur nach Bezeichnern fuer Datentypen. Aber genau darum geht es, ich will portabel entwickeln.


Anmelden zum Antworten