By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

partial specialization of function template

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Alexander Stippler wrote in news:3f******@news.uni-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/
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.