Zack Weinberg <[EMAIL PROTECTED]> writes:

| On Wed, 2005-05-25 at 03:03 +0200, Gabriel Dos Reis wrote:
| > Zack Weinberg <[EMAIL PROTECTED]> writes:
| > 
| > | On Tue, 2005-05-24 at 10:49 +0200, Gabriel Dos Reis wrote:
| > | > | So you don't see any value whatsoever to having (for instance) the
| > | > | individual constants of 'enum machine_mode' be inaccessible in most of
| > | > | GCC?  'cos I sure do.
| > | >
| > | > What I'm saying is that when you have a name like EXPAND_NORMAL, you
| > | > do not need to know the value it represents.  Just that it names a
| > | > constant.
| > | 
| > | We appear to be still talking about two different cases.  I am talking
| > | about the case where, in C++, you might declare something like
| > | 
| > | class machine_mode
| > | {
| > |   enum {
| > |     VOIDmode, SImode, // ...
| > |   } value;
| > | 
| > |  // accessors, whatever ...
| > | };
| > | 
| > | and then pass around 'machine_mode' objects.  Does this help?
| > 
| > Nothing in what I said or is proposing prevents that scenario, if you
| > decided to go there (despite pessimizations on x86 targets). 
| 
| This is still not an answer to the question I originally asked - do you

Oh this is getting silly, as I suggested the answer at the beginning
and repeated it over and over.

| see any way IN C to write code which has the relevant property of the
| class above (that is, that the FOOmode constants are not accessible
| except to authorized code) and which does not rely on free conversion
| between enumeration constants and integral types?

Yes.

  (1) Use the names.  What you want to prevent is the direct fidling
      with the bits, not the name.  Names are the messengers, not the
      message.  Don't shoot them.  So, no mocking around with type
      conversion unsigned int <-> enums.  When and if you move to
      class, codes using FOOmode will still work correctly.

   (2) When and if you switch to this:

        class machine_mode
        {
          enum value_t {
           VOIDmode, SImode, // ...
          } value;

         // accessors, whatever ...
        };

      You replace the global named constants with named constant objects

        extern const machine_mode FOOmode;

      This will not conflict with the other (enumeration values)
      FOOmode in machine_mode, because a class in C++ (unlike C)
      defines a scope, therefore shields them from the outside.  And
      people using FOOmode won't see any difference (except those
      breaking the abstraction barrier, but then you catch them).
      Furthermore, because you have access checking (brought to you for
      free) you do catch anyone who try to mock directly with the values.

So, using the named constants do make it easy for you if you move to
C++.  And if you decide not to go to C++, you still get better code.

-- Gaby

Reply via email to