home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!psinntp!newstand.syr.edu!rodan.acs.syr.edu!krnorton
- From: krnorton@rodan.acs.syr.edu (Karen R. Norton)
- Subject: Question Re. Integer Template Arguments
- Message-ID: <1992Dec12.175103.23139@newstand.syr.edu>
- Organization: Syracuse University, Syracuse, NY
- Date: Sat, 12 Dec 92 17:51:03 EST
- Lines: 50
-
-
- What I would like to do is something like the following:
- ------------------------------------------------------------------------
- template <int i> class C {
- ....
- };
-
- template <int i1, int i2> C<i1+i2> f(C<i1> c1, C<i2> c2) {
- return C<i1+i2>(....); //Call constructor with some args.
- }
- ------------------------------------------------------------------------
- Unfortunately even tho' this works with g++ 2.3.2, the ARM expressly
- forbids this (pg. 347 - "All template-args for a function template must be
- type-arguments.").
-
- An attempted work-around:
- ------------------------------------------------------------------------
- template <int i> class C {
- public: enum {D= i}; ....
- };
-
- template <class C1, class C2> C<C1::D + C2::D> f(C1 c1, C2 c2) {
- return C<C1::D + C2::D>(....); //Call constructor with some args.
- }
- ------------------------------------------------------------------------
- Unfortunately, g++ 2.3.2 gives an error when compiling the return type for
- f() - it claims that C1 and C2 are not aggregates. It has no problems with
- the similar construct used in the constructor in the return value (I proved
- this by instantiating the return type manually, while leaving the return
- value expression unchanged).
-
- I have the following questions:
-
- 1. What is the reason for the restriction on template-args for function
- templates? The rationale presented in the ARM appears to be too weak a
- reason for such a strong restriction. The restriction given on pg. 280 of
- Stroustrup2 ("Each template argument of a function template must affect the
- type of the function by affecting at least one argument type of functions
- generated from the template.") appears much more reasonable than the
- restriction given in the ARM.
-
- 2. Is my work-around incorrect C++, or does g++ have a problem?
-
- [If replying by email, please use the following email address, as I am
- posting from a friend's account on another machine].
-
- Thank you.
-
- Zerksis D. Umrigar
- (umrigar@bingsuns.cc.binghamton.edu)
-