IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

enum et type sous-jacent


Sujet :

Langage C++

  1. #1
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut enum et type sous-jacent
    Citation Envoyé par Lavock Voir le message
    Bon, par contre, un enum à plus de 256 valeur, ça craint.

  2. #2
    Membre confirmé Avatar de Lavock
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 560
    Points : 633
    Points
    633
    Par défaut
    Ben quoi, un enum, c'est un type int. Il est sûrement sur une machine 32 bit, mais rien n'interdit un int d'être un int8_t. Donc théoriquement, on devrait éviter les enum qui ne tienne pas dans des char non ?
    The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
    --Wilhelm Stekel

  3. #3
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Le type sous-jacent d'un enum est à la discrétion du compilateur. Il doit cependant en choisir un qui puisse contenir toutes les valeurs définies et au max un int ... sauf si le int n'est pas suffisant.
    En fait, sur GCC ou Visual, les enums sont souvent directement mappé sur des int.
    Notes qu'avec C++0x, tu pourras spécifier le type souhaité :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    enum answer : bool {yes, no}; // occupe la même place qu'un bool
    enum struct relation  : unsigned char {less, equal, greater}; // occupe la même place qu'un unsigned char
    enum class level : int {low, medium, high}; // occupe la même place qu'un int
    ce qui ne veut pas dire que le typage sera relâché :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
       unsigned char uc(0);
       relation r = uc; // erreur
       level le(level::low);
       int i = le;// erreur

  4. #4
    Membre confirmé Avatar de Lavock
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 560
    Points : 633
    Points
    633
    Par défaut
    Héhé, alors cela diffère du C :
    Citation Envoyé par ISO/IECC 9899 TC3 Committee Draft 2007-09-07, §6.7.2.2
    The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.
    Portant, je pense que j'aurais du mal a me défaire de cette habitude :'( !

    Note :
    Citation Envoyé par ISO/IECC N3000 2009-11-09, §7.2
    Each enumeration defines a type that is different from all other types. Each enumeration also has an
    underlying type. The underlying type can be explicitly specified using enum-base ; if not explicitly specified, the underlying type of a scoped enumeration type is int.
    PS : Il y a moyen de remplacer "Envoyé par" par autre chose ?
    The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
    --Wilhelm Stekel

  5. #5
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Citation Envoyé par Lavock Voir le message
    Héhé, alors cela diffère du C :


    Portant, je pense que j'aurais du mal a me défaire de cette habitude :'( !

    Note :

    PS : Il y a moyen de remplacer "Envoyé par" par autre chose ?
    C'est un peu plus subtil : 'scoped enumeration' désigne les nouvelles classes d'énumération (enum struct ou enum class) :
    The enumeration type declared with an enum-key of only enum is an unscoped enumeration, and its enumerators are unscoped enumerators. The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators.
    Pour les anciennes énumération (enum tout court), c'est la même définition que pour C++03 :
    For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can
    represent all the enumerator values defined in the enumeration. If no integral type can represent all the
    enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used
    as the underlying type except that the underlying type shall not be larger than int unless the value of an
    enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is
    as if the enumeration had a single enumerator with value 0.

  6. #6
    Membre confirmé Avatar de Lavock
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 560
    Points : 633
    Points
    633
    Par défaut
    Ok, en gros, en C, enum = int;
    en C++, enum = ce-que-le-compileur-décide mais max int;
    enum [struct/class] = ce-qu'on-veux.

    Question, quel est l'intérêt des scoped enum ?
    The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
    --Wilhelm Stekel

  7. #7
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Citation Envoyé par Lavock Voir le message
    Ok, en gros, en C, enum = int;
    en C++, enum = ce-que-le-compileur-décide mais max int;
    enum [struct/class] = ce-qu'on-veux.

    Question, quel est l'intérêt des scoped enum ?
    Bonne question : N237.

  8. #8
    Membre confirmé Avatar de Lavock
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 560
    Points : 633
    Points
    633
    Par défaut
    Merci bien !
    The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
    --Wilhelm Stekel

  9. #9
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Juste un petit aparté sur les énumérations en C, qui semble t'avoir induit en erreur:

    Citation Envoyé par Lavock Voir le message
    Il est sûrement sur une machine 32 bit, mais rien n'interdit un int d'être un int8_t.
    Si la plage minimale garantie pour un int (-32767 à 32767).

    En outre :

    5.2.4.1 Translation limits
    — 1023 enumeration constants in a single enumeration
    Donc, en C, tu peux avoir 1023 valeurs dans ton énumérations sans soucis.

  10. #10
    Membre confirmé Avatar de Lavock
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 560
    Points : 633
    Points
    633
    Par défaut
    Oui, mais leur valeur doivent être compris dans un int. Donc même si cela sont des int8, tu peux faire 1023 enumeration, mais avec seulement 256 valeurs...
    The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
    --Wilhelm Stekel

  11. #11
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Citation Envoyé par Lavock Voir le message
    Oui, mais leur valeur doivent être compris dans un int. Donc même si cela sont des int8
    Ca ne peut pas être un int8. 8 bits ne permettent pas de représenter la plage minimale garantie pour le type int.


    Citation Envoyé par Lavock Voir le message
    tu peux faire 1023 enumeration, mais avec seulement 256 valeurs...
    Ce n'est pas 1023 énumérations mais 1023 éléments (enumeration constants) dans une énumération.

  12. #12
    Membre confirmé Avatar de Lavock
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 560
    Points : 633
    Points
    633
    Par défaut
    Citation Envoyé par gl Voir le message
    Ca ne peut pas être un int8. 8 bits ne permettent pas de représenter la plage minimale garantie pour le type int.
    Ben, ça c'est un truc contradictoire. En gros, on pourrais pas faire du c ansi sur une plateforme 8bit >< ? Ou alors obligé de se farcir un type optimal pas optimal du tout ? Ca a toujours été comme ça ?

    Citation Envoyé par gl Voir le message
    Ce n'est pas 1023 énumérations mais 1023 éléments (enumeration constants) dans une énumération.
    Héhé, désolé, j'arrivais plus à trouver le mot.
    The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
    --Wilhelm Stekel

  13. #13
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Citation Envoyé par Lavock Voir le message
    Ben, ça c'est un truc contradictoire. En gros, on pourrais pas faire du c ansi sur une plateforme 8bit >< ? Ou alors obligé de se farcir un type optimal pas optimal du tout ? Ca a toujours été comme ça ?
    Salut,
    Les plages que doivent représentées les types numériques sont fixées par la norme C (99) et sont les mêmes pour C++. Et un int doit être au minimum 16bits. Est-ce que ça toujours été comme ça ? J'en sais rien. Le plus vieux proc. que j'ai tâté est le 8086 qui était déjà 16 bits.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment reconnaître le type sous-jacent d'un contrôle stylisé par un thème ?
    Par Chekov dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 27/11/2009, 15h44
  2. Laisser la requête sous-jacente à l'état ?
    Par marcoO dans le forum Requêtes et SQL.
    Réponses: 2
    Dernier message: 30/12/2006, 08h10
  3. Equivalent des table Enum de mysql sous Sql server
    Par scaleo dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 24/04/2006, 12h17
  4. [Sybase] Problème de type sous ASE
    Par Hotchotte dans le forum Sybase
    Réponses: 1
    Dernier message: 18/12/2004, 11h04
  5. Réponses: 3
    Dernier message: 20/12/2003, 19h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo