Topic: Using declaration and enumerations


Author: "denis bider" <denis.bider@abm.si>
Date: 1996/11/01
Raw View
In <199610312348.PAA06949@taumet.eng.sun.com>, clamage@taumet.eng.sun.com
(Steve Clamage) writes:
>
>A using-declaration just brings the specified name into the
>current scope. Bringing the name of the enum type into the current
>scope does not affect the visibility of the enumerators.
>

Why? It seems to me that the opposite would be more practical; just consider
an enum type with a hundred enumerators.



denis    [denis.bider@abm.si]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Brian Michael Freyburger <freyburg@gamut.stanford.edu>
Date: 1996/11/01
Raw View
"denis bider" <denis.bider@abm.si> writes:

>
> In <199610312348.PAA06949@taumet.eng.sun.com>, clamage@taumet.eng.sun.com
> (Steve Clamage) writes:
> >
> >A using-declaration just brings the specified name into the
> >current scope. Bringing the name of the enum type into the current
> >scope does not affect the visibility of the enumerators.
> >
>
> Why?

Consistency with:a using declaration of a class name, which doesn't
bring it's members (even the static ones) into scope.

> It seems to me that the opposite would be more practical; just consider
> an enum type with a hundred enumerators.

I think the best you could hope for is:

using enum foo;

Which would bring the enumerator name and all the constants into
scope, which sounds useful, but, I would imagine, is unlikely to be
adopted at this stage.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: clamage@taumet.eng.sun.com (Steve Clamage)
Date: 1996/11/02
Raw View
In article TAA01471@blue.abm.si, "denis bider" <denis.bider@abm.si> writes:
>In <199610312348.PAA06949@taumet.eng.sun.com>, clamage@taumet.eng.sun.com
>(Steve Clamage) writes:
>>
>>A using-declaration just brings the specified name into the
>>current scope. Bringing the name of the enum type into the current
>>scope does not affect the visibility of the enumerators.
>
>Why? It seems to me that the opposite would be more practical; just consider
>an enum type with a hundred enumerators.

It would require a special language rule for using-declarations with the
name of an enum type, different from using-declarations for anything else.
A special-case language rule ought to solve a serious problem which
can't be reasonably dealt with otherwise, and should not introduce new
problems. (Unfortunately, I think we already have some special case
rules that don't meet this two-part test. That doesn't mean we should
add still more.)

Now consider this case:

 namespace N {
  enum state { off, on };
 }
 namespace M {
  typedef N::state status;
 }
 using N::state;

Suppose we say that the using-declaration brings N::off and N::on into the
current scope. Now change the last line to this:
 using M::status;
Whether it does or does not bring N::off and N::on into the current scope,
it seems to me that we have an inconsistency of some sort.

Someone else made a comparison with classes, pointing out that "using"
a class name doesn't bring the class members into the current scope.
That isn't a valid comparison, since a class creates a new scope,
but an enum does not. The enumerators are in the same scope as
the enum name.

Instead of any of the above, you could put the enum into its own namespace:
 namespace state {
  enum state_t { off, on };
 }

Now a single using-directive
 using namespace state;
makes all the names visible in the current scope.

---
Steve Clamage, stephen.clamage@eng.sun.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/04
Raw View
"denis bider" <denis.bider@abm.si> writes:

>clamage@taumet.eng.sun.com (Steve Clamage) writes:
>>
>>A using-declaration just brings the specified name into the
>>current scope. Bringing the name of the enum type into the current
>>scope does not affect the visibility of the enumerators.
>
>Why? It seems to me that the opposite would be more practical; just consider
>an enum type with a hundred enumerators.

As a work-around, you can use the following technique: put
each enum in its own nested namespace.  Instead of writing

 namespace bar {
  ...
  enum Foo { Foo1, Foo2, ..., Foo100 };
 };

write

 namespace bar {
  ...
  namespace foo {
   enum Foo { Foo1, Foo2, ..., Foo100 };
  };
  using namespace foo;
 };

Then, a user of this namespace can write either

 using bar::Foo;   // just imports the enum type name

or

 using namespace bar::foo; // imports both the enum type name
     // and all the enumerators

as they wish.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Michael Rice" <mpr@absoft.com>
Date: 1996/10/31
Raw View
When I use a using declaration to access an enumeration in some
namespace, are the enumerators made accessible too?  Should they
be? [Example:

namespace N {
  enum E { a, b, c };
}

int main()
{
  using N::E;   // E a synonym for N::E

  E e1 = a;     // Ok?

  E e2 = N::a;  // Or do I have to do this?

  return 0;
}

--end example]

--
Mike Rice (mpr@absoft.com)
Absoft Corporation
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: clamage@taumet.eng.sun.com (Steve Clamage)
Date: 1996/11/01
Raw View
In article AA04531@absoft.absoft.com, "Michael Rice" <mpr@absoft.com> writes:
>When I use a using declaration to access an enumeration in some
>namespace, are the enumerators made accessible too?  Should they
>be?

No. A using-declaration just brings the specified name into the
current scope. Bringing the name of the enum type into the current
scope does not affect the visibility of the enumerators.

> [Example:
>
>namespace N {
>  enum E { a, b, c };
>}
>
>int main()
>{
>  using N::E;   // E a synonym for N::E
>
>  E e1 = a;     // Ok?

No.

>  E e2 = N::a;  // Or do I have to do this?

Yes.

---
Steve Clamage, stephen.clamage@eng.sun.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]