home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!mcgrant
- From: mcgrant@leland.Stanford.EDU (Michael Grant)
- Subject: typeof operator?
- Message-ID: <1992Nov10.092148.430@leland.Stanford.EDU>
- Followup-To: comp.lang.c++
- Summary: Good thing for templates to have
- Keywords: typeof templates
- Organization: Information Systems Laboratory, Stanford University
- Date: Tue, 10 Nov 92 09:21:48 GMT
- Lines: 62
-
- Has anyone in the C++ committee considered inclusion of a typeof()
- operator into standard C++? It would certainly make for some very
- interesting templates.
-
- The typeof() operator exists in GNU g++, but I'm not sure it can
- be used in the following manner. I would certainly hope it could.
-
- Consider:
-
- template<class T>
- class Foo {
- T num;
- typeof(abs(T)) len;
- ...
- }
-
- Now, if 'double abs(double)' were defined in-scope, Foo<double> would yield
-
- class Foo<double> {
- double num;
- double len;
- ...
- }
-
- But, if 'double abs(complex)' were defined in-scope, Foo<complex> would
- yield
-
- class Foo<complex> {
- complex num;
- double len;
- ...
- }
-
- Isn't that great? For readability's sake when writing member functions,
- it would make sense to do something like
-
- template <class T>
- class Foo {
- typedef typeof(abs(T)) absType;
- complex num;
- absType len;
- ...
- }
-
- That way, inside member functions I can use Foo<T>::absType to refer to
- the type of that variable. And, in more complicated situations, it seems
- convenient for the template class to automatically track the changing
- output types of the functions it uses.
-
- Any comments? Of course, this can be MANUALLY simulated with things like
-
- template <class T,class absT>
- class Foo {
- T num;
- absT len;
- }
-
- but it seems that this is a natural extension to parametrized types.
-
- Michael C. Grant
- mcgrant@isl.stanford.edu
-
-