partial specialization of function template

A

Alexander Stippler

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
 
R

Rob Williscroft

Alexander Stippler wrote in
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top