This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: templates
- From: Nathan Sidwell <nathan at codesourcery dot com>
- To: Adrian Sandor <aditsu at yahoo dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 11 Feb 2003 16:47:20 +0000
- Subject: Re: templates
- References: <20030211075946.95885.qmail@web40307.mail.yahoo.com>
Adrian Sandor wrote:
let's say I have a simple template function:
template<class T>T sqr(T x){return x*x;}
then if I write "sqr(5)" then everything is ok
but if I make some changes:
template<class T>struct dummy{typedef T type;};
template<class T>T sqr(typename dummy<T>::type
x){return x*x;}
then "sqr(5)" doesn't compile anymore; instead I have
to write "sqr<int>(5)"
what is the cause of this behaviour and how can I
avoid it? (I need to use some type traits)
in handwaving terms, because template deduction doesn't recurse.
Consider if you had
template<typename T> struct dummy{typedef T type;}
template<> struct dummy<int> {typedef float type;}
that would be hard.
template <typename T> typename dummy<T>::type sqr (T);
would be deducible
please refer to a C++ news group/faq/list for further explanation
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
The voices in my head said this was stupid too
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org