Topic: using myclass::mytypedef;' valid?


Author: gregory.l.priem@intel.com (Gregory L Priem)
Date: 1998/11/27
Raw View
i think that the following is valid c++ code, but i have not yet
mastered interpreting the iso c++ standard document.

in order to avoid polluting the global namespace in my header files, i
like to put typedefs in the class declaration, like the following:

class MyClass {
public:
  // public typedefs
  typedef std::vector<std::string> MyStringContainer;

  // ctor, dtor, etc...
  MyClass(const MyStringContainer& strings);

  // rest of class decl follows

};

then, in my code that uses MyClass, i just have:

using MyClass::MyStringContainer;

this allows me to use the typedef which was used in the class, so if
it changes, my code will not have to be changed [unless of course i
depended on the fact that it was originally implemented with a vector,
and thus had random iterators, but that code would need changing
regardless.]

is this valid c++? one of my compilers [microsoft vc5 sp2] accepts
this just fine, but another [egcs 1.1] does not, and usually that
means to me that i am doing something wrong or else it is a compiler
issue. [egcs generated an internal compiler error, so i sent in a bug
report, but that does not mean that my code is actually valid.]


greg priem                         kb0erz
gregory.l.priem@intel.com
bark, fido, bark [neither of us speak for my employer]
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/11/28
Raw View
I think you are confusing class scope with namespace scope.  In order to
achieve what you want you must place your typedefs in a namespace, not
in a class.  There are no using declarations for class scope identifiers
except for the special case where a derived class wants to import an
identifier from a base class.

Just rewrite your code as:
namespace MyClassNS {
        // typedefs, enums etc.
        class MyClass {
                // class definition
        };
}

Now you can introduce the short versions of your typedef identifiers etc
via using declarations.


In article <366020dd.728988@news.fm.intel.com>, Gregory L Priem
<gregory.l.priem@intel.com> writes
>i think that the following is valid c++ code, but i have not yet
>mastered interpreting the iso c++ standard document.

>in order to avoid polluting the global namespace in my header files, i
>like to put typedefs in the class declaration, like the following:

>class MyClass {
>public:
>  // public typedefs
>  typedef std::vector<std::string> MyStringContainer;
>
>  // ctor, dtor, etc...
>  MyClass(const MyStringContainer& strings);
>
>  // rest of class decl follows

>};

>then, in my code that uses MyClass, i just have:

>using MyClass::MyStringContainer;

>this allows me to use the typedef which was used in the class, so if
>it changes, my code will not have to be changed [unless of course i
>depended on the fact that it was originally implemented with a vector,
>and thus had random iterators, but that code would need changing
>regardless.]

>is this valid c++? one of my compilers [microsoft vc5 sp2] accepts
>this just fine, but another [egcs 1.1] does not, and usually that
>means to me that i am doing something wrong or else it is a compiler
>issue. [egcs generated an internal compiler error, so i sent in a bug
>report, but that does not mean that my code is actually valid.]

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/11/28
Raw View
Gregory L Priem wrote:
>
> i think that the following is valid c++ code, but i have not yet
> mastered interpreting the iso c++ standard document.
>
> in order to avoid polluting the global namespace in my header files, i
> like to put typedefs in the class declaration, like the following:
>
> class MyClass {
> public:
>   // public typedefs
>   typedef std::vector<std::string> MyStringContainer;
>
>   // ctor, dtor, etc...
>   MyClass(const MyStringContainer& strings);
>
>   // rest of class decl follows
>
> };
>
> then, in my code that uses MyClass, i just have:
>
> using MyClass::MyStringContainer;
>
> this allows me to use the typedef which was used in the class, so if
> it changes, my code will not have to be changed [unless of course i
> depended on the fact that it was originally implemented with a vector,
> and thus had random iterators, but that code would need changing
> regardless.]

AFAIK, using classname::identifier is only allowed in derived
classes, while otherwise you can only use
using namespace_name::identifier.

However, there's a simple solution for your problem:

typedef MyClass::MyStringContainer MyStringContainer;

[...]


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]