Topic: Member typedefs and dot syntax
Author: Rob Stewart <donotspamme@giage.com>
Date: 2000/05/11 Raw View
Patrick Smith wrote:
>
> Ron Natalie wrote:
> > Patrick Smith wrote:
> > > because f.bar is not a valid typedef-name. Gcc (2.95.2) seems to agree
> > > with me.
> >
> > I'd say that it was because bar is not a valid MEMBER name to be applied to
> > the right-hand side of the "." operator. But the truth of the matter, is
> > that it makes no sense.
>
> Why does it make no sense? You can use the dot syntax to access a
> static data member. It seems natural to me to want to use it to access
> a typedef.
The right analogy is to a nested class. It is referenced using
the scope resolution operator, not the member selection operator.
--
Robert Stewart | rob-at-giage-dot-com
Software Engineer | using std::disclaimer;
Giage, Inc. | http://www.giage.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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Patrick Smith <patsmith@pobox.com>
Date: 2000/04/28 Raw View
In my reading of the standard, the following is illegal:
struct foo {
typedef int bar;
};
void baz(foo f) {
f.bar x = 0;
}
because f.bar is not a valid typedef-name. Gcc (2.95.2) seems to agree
with me.
Question 1: Is this in fact illegal?
Of course, in this example, the fix is trivial: use foo::bar instead.
But it would be nice to be able to do this without knowing the type of
f. For example,
#define FOR_EACH(i, c) \
for (c.iterator i = c.begin(); i != c.end(); i++)
Question 2: Is this possible in standard C++?
It is possible using gcc extensions:
template<class T> struct the {
typedef T type;
};
void baz(foo f) {
the<__typeof__(f)>::type::bar x = 0;
}
(gcc doesn't like the simpler '__typeof__(f)::bar x = 0;', however.)
--
patsmith@pobox.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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/04/28 Raw View
Patrick Smith wrote:
>
> because f.bar is not a valid typedef-name. Gcc (2.95.2) seems to agree
> with me.
I'd say that it was because bar is not a valid MEMBER name to be applied to
the right-hand side of the "." operator. But the truth of the matter, is
that it makes no sense.
> Of course, in this example, the fix is trivial: use foo::bar instead.
> But it would be nice to be able to do this without knowing the type of
> f. For example,
>
> #define FOR_EACH(i, c) \
> for (c.iterator i = c.begin(); i != c.end(); i++)
I don't understand, why not use a template for FOR_EACH, There's even one
already in the library.
---
[ 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: Patrick Smith <patsmith@pobox.com>
Date: 2000/04/28 Raw View
Ron Natalie wrote:
> Patrick Smith wrote:
> > because f.bar is not a valid typedef-name. Gcc (2.95.2) seems to agree
> > with me.
>
> I'd say that it was because bar is not a valid MEMBER name to be applied to
> the right-hand side of the "." operator. But the truth of the matter, is
> that it makes no sense.
Why does it make no sense? You can use the dot syntax to access a
static data member. It seems natural to me to want to use it to access
a typedef.
> > For example,
> >
> > #define FOR_EACH(i, c) \
> > for (c.iterator i = c.begin(); i != c.end(); i++)
>
> I don't understand, why not use a template for FOR_EACH, There's even one
> already in the library.
This was just an example; it's almost irrelevant to my question (is
there a way to achieve this effect in standard C++).
But still - I would only rarely use the for_each template for a loop.
Because to do so, I would have to put the body of the loop into a
function, adding clutter to my program. To access local variables
defined outside the loop would require even more clutter. I'd rather
just write
for (iterator_type i = c.begin(); i != c.end(); i++)
directly. But given that this gets written so often, a macro for it
would be nice. And it would be nice not to have to give the iterator
type as a third parameter to the macro.
--
patsmith@pobox.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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/29 Raw View
Patrick Smith wrote:
....
> But still - I would only rarely use the for_each template for a loop.
> Because to do so, I would have to put the body of the loop into a
> function, adding clutter to my program. To access local variables
Keep in mind that the standard provides lots of standard function
objects, and very often you'll be able to use one of them as the loop
body. Figuring out that one of them is applicable is non-trivial, but
can be learned.
---
[ 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 ]