Topic: accessing typedefs through object instance


Author: Raja R Harinath <harinath@cs.umn.edu>
Date: 1997/03/18
Raw View
fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) writes:
> "Sean L. Palmer" <seanpalmer@mindspring.com> writes:
> >class A {
> >  typedef int B;
> >};
> >A a;
> >a.B b2;    //fails in my compiler
> [...]
> >So anyway I remember people talking about this before and I was wondering
> >what happened with that, if it is now in the standard or not, and what
> >reasons were given for not including it if it didn't make it.
>
> It is not in the draft standard.  Allowing the above would create a lot of
> parsing ambiguities that are reportedly very difficult to resolve.

Which parsing problems could be averted by tagging it with `typename',
like in

  typename a.B b2;

This syntax extends `typename' to recognize a scope "travelling"
operator (.) in addition to the one it already knows (::).

> Allowing
>
>  typeof(a)::B b2;
>
> would be easier.  GNU C++ supports this, but it is not standard.
> I don't think `typeof' was ever formally proposed to the C++ committee.

`typename' and its use above should probably be simpler to specify as
compared to introducing `typeof', as `typeof' carries more baggage.

The other main use of `typeof'

  typeof(a) b;

usually occurs in pre-processor macros, and this usage is largely
overshadowed by templates.

- Hari

--
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash
---
[ 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: "Sean L. Palmer" <seanpalmer@mindspring.com>
Date: 1997/03/16
Raw View
I remember hearing people talking about being able to access member
typedefs and such through an object instance of that class, something of
this nature:

class A {
  typedef int B;
};

A a;

A::B b1;   //ok

a.B b2;    //fails in my compiler

There are reasons why you'd want access to things like that. For instance,
if the class in question has a member typedef such as this_type which
equates to the type of the class itself. And for accessing member types and
such, stuff like iterators, when given only a class object.  Here's
something that demonstrates what I'm trying to accomplish:

#define for_each(container,id) \
  for (container.iterator id(container.begin()); id!=container.end(); ++id)

Which would allow:

#include <vector>
#include <iostream>

vector<int> iv;

for_each(iv,i) {
  cout << *i << ' ';
}


Currently this sort of thing is not possible and I also have to pass in the
class of container, in addition to the actual object variable identifier,
which I think should I should be able to infer type from in this case.

This would require a simple extension to the language, such that any static
class member such as a typedef can be accessed as if it were a member of a
structure, as well as by :: notation.  Alternatively, in the construct
ident::member, ident could be either a type identifier, or an identifier of
an instance of a type,  in which case the type is inferred.  I suggest that
either dot notation or :: notation be usable.

I realize that some of this functionality is available by using templates,
but currently templates aren't flexible enough to handle many cases...

So anyway I remember people talking about this before and I was wondering
what happened with that, if it is now in the standard or not, and what
reasons were given for not including it if it didn't make it.
---
[ 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: 1997/03/16
Raw View
"Sean L. Palmer" <seanpalmer@mindspring.com> writes:

>class A {
>  typedef int B;
>};
>A a;
>a.B b2;    //fails in my compiler
[...]
>So anyway I remember people talking about this before and I was wondering
>what happened with that, if it is now in the standard or not, and what
>reasons were given for not including it if it didn't make it.

It is not in the draft standard.  Allowing the above would create a lot of
parsing ambiguities that are reportedly very difficult to resolve.

Allowing

 typeof(a)::B b2;

would be easier.  GNU C++ supports this, but it is not standard.
I don't think `typeof' was ever formally proposed to the C++ committee.

--
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 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                             ]