Topic: Q: templates and typedefs
Author: Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr>
Date: 1998/10/11 Raw View
/*
newsgroups: comp.lang.c++,
comp.lang.c++.moderated,
egcs@cygnus.com, egcs-bugs@cygnus.com
bug-g++@prep.ai.mit.edu
gnu.g++.bug, gnu.g++.help, gnu.g++.lib.bug
comp.std.c++
reply to : vcvjetko@linux.rulz.zg.tel.hr
by removing praising the best OS
Hi again!
This time I'm trying o figure out future plans for template
typedefs (aliases), for example for class names in different
languages (english: Event, german: Ereignis).
I'd like to define a template alias for a template class, just
like for built in types in following exmple.
I know that I could solve it
through inheritance (making alias derived class, but then
it's not the same class, rather specialization)
or
by using preprocesor directives # defines (which is
according to type-checking not appropriate.
*/
template <class T>
class X
{
T t_;
};
typedef int whole_number;
typedef X<int> Xint;
template<class Type> typedef X<Type> Alias_for_X<Type>;
int main()
{
whole_number wn;
Xint xi;
Alias_for_X<Type>
return 0;
}
/*
Here are compile outputs:
1. gcc 2.8.1
Reading specs from /usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/specs
gcc version 2.8.1
/usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/cpp -lang-c++ -v -I
/home/gcl/gcl/include -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus
-D__GNUC_MINOR__=8 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__
-D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -g -Wall
-Di386 -Di586 -Asystem(unix) -Acpu(i386) -Amachine(i386) -D__i386__
-D__i586__ -Asystem(unix) -Acpu(i386) -Amachine(i386)
/home/gcl/gcl/example/Tests/template_typedefs.cpp /tmp/cca21947.ii
GNU CPP version 2.8.1 (i386 GNU/Linux with ELF)
#include "..." search starts here:
#include <...> search starts here:
/home/gcl/gcl/include
/usr/include/g++
/usr/lib/g++-include
/usr/local/include
/usr/i586-pc-linux-gnulibc1/include
/usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/include
/usr/include
End of search list.
/usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/cc1plus /tmp/cca21947.ii
-quiet -dumpbase template_typedefs.cc -g -Wall -version
-ftemplate-depth-45 -o /tmp/cca21947.s
GNU C++ version 2.8.1 (i586-pc-linux-gnulibc1) compiled by GNU C version
2.8.1.
/home/gcl/gcl/example/Tests/template_typedefs.cpp:15: sorry, not
implemented: `template_id_expr' not supported by dump_decl
/home/gcl/gcl/example/Tests/template_typedefs.cpp:15: template
declaration of `typedef class X<T> '
/home/gcl/gcl/example/Tests/template_typedefs.cpp: In function `int
main()':
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: `Type' undeclared
(first use this function)
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: (Each undeclared
identifier is reported only once
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: for each function
it appears in.)
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: type/value
mismatch at argument 0 in template parameter list for `template <class
Type> X<T>'
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: expected a type,
got `{error}'
/home/gcl/gcl/example/Tests/template_typedefs.cpp:23: parse error before
`return'
/home/gcl/gcl/example/Tests/template_typedefs.cpp:20: warning: unused
variable `class X<int> xi'
/home/gcl/gcl/example/Tests/template_typedefs.cpp:19: warning: unused
variable `int wn'
2. egcs-1.1b
Reading specs from
/usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
/usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/cpp -lang-c++
-v -I /home/gcl/gcl/include -undef -D__GNUC__=2 -D__GNUG__=2
-D__cplusplus -D__GNUC_MINOR__=91 -D__ELF__ -Dunix -Dlinux -D__ELF__
-D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS
-g -Wall -Asystem(unix) -Acpu(i386) -Amachine(i386) -Di386 -D__i386
-D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium
-D__pentium__ /home/gcl/gcl/example/Tests/template_typedefs.cpp
/tmp/cclEGAqb.ii
GNU CPP version egcs-2.91.57 19980901 (egcs-1.1 release) (i386
Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/home/gcl/gcl/include
/usr/egcs/include/g++
/usr/local/include
/usr/egcs/i586-pc-linux-gnulibc1/include
/usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/include
/usr/include
End of search list.
/usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/cc1plus
/tmp/cclEGAqb.ii -quiet -dumpbase template_typedefs.cc -g -Wall -version
-ftemplate-depth-45 -o /tmp/ccOJPNpk.s
GNU C++ version egcs-2.91.57 19980901 (egcs-1.1 release)
(i586-pc-linux-gnulibc1) compiled by GNU C version egcs-2.91.57 19980901
(egcs-1.1 release).
/home/gcl/gcl/example/Tests/template_typedefs.cpp:15: template
declaration of `typedef class X<T> Alias_for_X<Type>'
/home/gcl/gcl/example/Tests/template_typedefs.cpp: In function `int
main()':
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: `Type' undeclared
(first use this function)
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: (Each undeclared
identifier is reported only once
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: for each function
it appears in.)
/home/gcl/gcl/example/Tests/template_typedefs.cpp:21: template argument
1 is invalid
/home/gcl/gcl/example/Tests/template_typedefs.cpp:23: parse error before
`return'
/home/gcl/gcl/example/Tests/template_typedefs.cpp:20: warning: unused
variable `class Xint xi'
/home/gcl/gcl/example/Tests/template_typedefs.cpp:19: warning: unused
variable `whole_number wn'
Does anybody know if it's gonna be implemented or what are
the limitations for it and best way to solve this problem in the
near and far future!! We need it for our future plans.
Thanx a lot for your help
miljenko
*/
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/10/12 Raw View
Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr> wrote:
> This time I'm trying o figure out future plans for template
> typedefs (aliases) ...
> I'd like to define a template alias for a template class, just
> like for built in types in following exmple.
So would we all...
>template <class T> class X { T t_; };
>template<class Type> typedef X<Type> Alias_for_X<Type>;
This looks like a good guess for what the syntax will be when someone
implements it. Probably it is what will be adopted ten years from now.
(Incidentally, there's hardly any reason ever to run any
code throught gcc-2.8.x. It's a dead development line.)
> Does anybody know if it's gonna be implemented or what are
> the limitations for it and best way to solve this problem in the
> near and far future!! We need it for our future plans.
Until there is template typedef you must do something less pretty:
template <class T>
struct Alias_for_X { typedef X<T> type; };
To use it, instead of
Alias_for_X<T>
as you would say if template typedef worked, you say:
Alias_for_X<T>::type
You might have to add "typename" on the front sometimes:
typename Alias_for_X<T>::type
Normally this method is called "rebind". It's better than nothing.
You can do something more powerful if you have template template
arguments:
template <template <class T> class A, class U>
struct Alias { typedef A<U> type; };
Then you can say
typename Alias<X,T>::type
which could sometimes be almost as good as the real thing.
(Egcs has template template arguments.)
[I have set followup-to to c.s.c++ and c.l.c++. It's generally
a mistake to cross-post to two moderated newsgroups.]
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ 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: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/10/12 Raw View
On 12 Oct 1998 16:07:10 GMT, Nathan Myers <ncm@nospam.cantrip.org> wrote:
>Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr> wrote:
>>template<class Type> typedef X<Type> Alias_for_X<Type>;
>This looks like a good guess for what the syntax will be when someone
>implements it. Probably it is what will be adopted ten years from now.
I'm not a template expert, but isn't a typedef supposed to do semantic
checking on the template? Which would mean that it needs a concrete
type. IOW, myspace::X<typename> cannot be checked for semantic errors,
but myspace::X<int> can.
Anyway, this technique would be very nice to use with function
definitions. Consider the following function declaration:
template <class Function>
typename Function::result_type
inline
simpson
(Function f,
typename Function::argument_type x1,
typename Function::argument_type x3);
BTW, I'm not sure if the 'typename' is necessary, but I think it is.
Now the same thing using typedefs:
template <class Function>
typedef typename Function::result_type result_type ;
typedef typename Function::argument_type argument_type;
result_type inline simpson(Function f, argument_type x1, argument_type x3);
The typedefs are local only to the function declaration and/or
definition. But we should have another shorthand:
template <class Function>
typedef typename Function::typename typename;
result_type inline simpson(Function f, argument_type x1, argument_type x3);
>(Incidentally, there's hardly any reason ever to run any
>code throught gcc-2.8.x. It's a dead development line.)
Yes 'egcs' from www.cygnus.com is good.
>[I have set followup-to to c.s.c++ and c.l.c++. It's generally
>a mistake to cross-post to two moderated newsgroups.]
Just curious, why?
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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 ]