Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > partial specialization of function template

Reply
Thread Tools

partial specialization of function template

 
 
Alexander Stippler
Guest
Posts: n/a
 
      08-15-2003
Hi,

the following code does not compile. Why and how can I reach the desired
effect?

template <int n, typename T>
double
norm(const T &t)
{ return 0.0; }

template <typename T>
double
norm<2,T>(const T &)
{ return 2.0; }

class A
{
};

int
main()
{
A a;
norm<2>(a);

return 0;
}

regards,
alex

 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      08-15-2003
Alexander Stippler wrote in news:(E-Mail Removed)-ulm.de:

> Hi,
>
> the following code does not compile. Why and how can I reach the
> desired effect?
>
> template <int n, typename T>
> double
> norm(const T &t)
> { return 0.0; }
>
> template <typename T>
> double
> norm<2,T>(const T &)
> { return 2.0; }
>
> class A
> {
> };
>
> int
> main()
> {
> A a;
> norm<2>(a);
>
> return 0;
> }
>


#include <iostream>
#include <ostream>

template < int N, typename T>
struct do_norm
{
static double apply( T const &t )
{
return 0.0;
}
};

template < typename T >
struct do_norm< 2, T >
{
static double apply( T const &t )
{
return 2.0;
}
};

template < int N, typename T>
double norm( T const &t )
{
return do_norm< N, T >::apply( t );
}


struct A {};

int main()
{
A a;
std::cout << norm<2>(a) << std::endl;
std::cout << norm<1>(1) << std::endl;
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template template partial specialization Hizo C++ 17 03-07-2011 08:09 AM
how to do partial specialization template for non member method template vj C++ 1 12-20-2010 11:57 AM
syntax problem for partial template specialization for a template class. toton C++ 1 12-28-2006 04:08 PM
partial specialization of template member function Levent C++ 5 05-10-2005 09:35 PM
Partial template specialization on a function mrstephengross C++ 1 04-14-2005 02:15 PM



Advertisments