Topic: Is there a typeof-construct?
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/06/25 Raw View
willer@sq.com (Steve Willer) writes:
> #define RETURN(val) { cout << "Returning " << val; return val; }
>
> The problem with this macro is that "val" is evaluated twice, so I can't
> really put function calls in the argument to RETURN. If I had a typeof()
> operator available to me that returned the type of the return value in
> the case of functions, I could write:
>
> #define RETURN(val) { typeof(val) &__ret; cout << "Returning " <<\
> __ret; return __ret; }
How about:
template <class T> inline T return_value(T ret) {
cout << "Returning " << ret; return ret;
}
#define RETURN(val) return(return_value(val));
--
Alexandre Oliva
oliva@dcc.unicamp.br
Universidade Estadual de Campinas, S~ao Paulo, Brasil
---
[ 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: dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann)
Date: 1996/06/19 Raw View
Hello!
Is there any form of typeof(expression) construct, which is evaluated at
compile time (like the sizeof operator)?
Example for the usage: (Don't mind any syntactic mistakes.)
class X
{
void f();
someContainerClass a;
};
X::f()
{
someCode...;
for (typeof(a)::iterator i = a.begin(); i < a.end; i++) {
someCode...;
};
someCode...;
}
The idea is, that I might not be sure which container class to choose.
If the code is written that way, I could easily switch between different
container classes and only had to modify the class declaration (given
that I do not make further assumptions on the container class and the
iterator).
It's clear, that one could as well use a typedef there, but this would
introduce another name, what I like to avoid.
Maybe there is a different concept for what I am trying? For example,
could I use a.iterator as a typename? If one can access public member
variables that way, can one access public types the same way? The above
code then looks like:
X::f()
{
someCode...;
for (a.iterator i = a.begin(); i < a.end; i++) {
someCode...;
};
someCode...;
}
This might even be a better solution.
Regards,
Dirk Herrmann
---
[ 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: willer@sq.com (Steve Willer)
Date: 1996/06/19 Raw View
dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann) wrote:
>Maybe there is a different concept for what I am trying? For example,
>could I use a.iterator as a typename?
Basically, you could have to put a typedef inside the class. For
example:
class Foo; // defined somewhere
struct myclass {
...
typedef vector<foo> FooList;
typedef FooList::iterator FooIterator;
FooList vFoo;
}
void Bar(myclass &x) {
for (myclass::FooIterator i = x.vFoo.begin();
i != x.vfoo.end(); ++i) {
// some code
}
}
Unfortunately, it's messy because you have to explicitly give the type
of the argument as well as the argument itself, which introduces
redundancy, which introduces potential errors, etc. But it's solvable.
The reason the lack of typeof() really irks me is that I've needed to
implement macros similar to this one:
#define RETURN(val) { cout << "Returning " << val; return val; }
The problem with this macro is that "val" is evaluated twice, so I can't
really put function calls in the argument to RETURN. If I had a typeof()
operator available to me that returned the type of the return value in
the case of functions, I could write:
#define RETURN(val) { typeof(val) &__ret; cout << "Returning " <<\
__ret; return __ret; }
...but it's not really the end of the world, and I doubt the language
will have this added, so I'll just have to live with this minor problem.
>X::f()
>{
> someCode...;
> for (a.iterator i = a.begin(); i < a.end; i++) {
> someCode...;
> };
> someCode...;
>}
Wouldn't this be nice. The only problem is that the developer might get
confused about whether it's a type member or a "real" member (although
this can happen with anything and the compiler would give an error
anyway). Actually, to avoid this confusion, perhaps the typename
operator can be reused like this:
void X::f() {
for (typename(a.iterator) i = a.begin(); i != a.end(); ++i) {
// some code
}
}
Just a thought, anyway.
----
Steve Willer, SoftQuad International, Toronto ON
Work: willer@sq.com Personal: willer@interlog.com CIS: 70400,3667
[ 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: willer@sq.com (Steve Willer)
Date: 1996/06/20 Raw View
willer@sq.com (Steve Willer) wrote:
>The problem with this macro is that "val" is evaluated twice, so I can't
>really put function calls in the argument to RETURN. If I had a typeof()
>operator available to me that returned the type of the return value in
>the case of functions, I could write:
>
>#define RETURN(val) { typeof(val) &__ret; cout << "Returning " <<\
> __ret; return __ret; }
Someone just pointed out an alternative form to me:
template <class T>
inline const T& __return_helper(const T& ref, ostream &ostr) {
ostr << "Returning " << ref << "\n";
return ref;
}
#define RETURN(val) return __return_helper((val),cout)
This seems like it should work, and this was my major reason for wanting
typeof().
So I take it all back. Never mind.
>void X::f() {
> for (typename(a.iterator) i = a.begin(); i != a.end(); ++i) {
> // some code
> }
>}
My solution to this has always been a combination of typedefs, naming
conventions, a couple of modifications to my STL, and some home-grown
macros. I would write:
typedef list<Foo> FooList;
typedef FooList::iterator FooIterator;
void X::f(FooList &vFoo)
{
for_iter(vFoo, FooIterator) {
cout << iter->Name(); // or whatever
}
}
----
Steve Willer, SoftQuad International, Toronto ON
Work: willer@sq.com Personal: willer@interlog.com CIS: 70400,3667
---
[ 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
]