This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: templates


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]