Topic: [??] Template parameters: can one depend on a previous one?


Author: biderd@abm.si
Date: 1996/08/24
Raw View
Is a template declaration such as below valid? (My compiler doesn't support it,
and I couldn't find anything explicit in the April 95 draft)

    template<class T, T value>   // The second param. depends on the first one
    class Named {
        operator T& const () const { return value; }
    };

If it is,
    Named<String, "Trans aequora abiit Chloe"> Blah;
could be used instead of
    #define Blah String("Trans aequora abiit Chloe")

The first version obeys scope rules, while the #define version doesn't.


denis (denis.bider@abm.si)
---
[ 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: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/08/26
Raw View
>>>>> "B" == biderd  <biderd@abm.si> writes:
[...]
B>     template<class T, T value>   // The second param.
                                    // depends on the first one
B>     class Named {
B>         operator T& const () const { return value; }
B>     };

I believe it is OK (I vaguely remember a similar example in the DWP),
but I'm not positive. If it is, it certainly constrains the T types
for which you can instantiate this.

B> If it is,
B>     Named<String, "Trans aequora abiit Chloe"> Blah;
B> could be used instead of
B>     #define Blah String("Trans aequora abiit Chloe")

Surely not. "Trans aequora abiit Chloe" is of type char const*
(since Stockholm, previously char*), not String. Remember that
template instantiation is a compile-time thing --- it's not
reasonable to expect compile-time invocation of user- or
library-defined conversions.

 Daveed
---
[ 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                             ]