Template Default Arguements

// How to pass default arguments to function templates. ?

#include <iostream>
using namespace std;

// template<typename A=int,typename B>
//Code Produces error if above line is uncommented

template<typename A,typename B>
void function(A,B);

int main()
{
function(5,3.2);
return 0;
}


template<typename A,typename B>
void function(A a,B b)
{
cout <<"Sum: " << a+b << endl;
}
Last edited on
closed account (zb0S216C)
Default template arguments are only allowed on class definitions. Also, template arguments (typename) must be on the right-hand side. For example:

1
2
3
4
5
6
7
8
9
template < typename T = int, typename P > // Wrong!
class CLASS
{
};

template < typename P, typename T = int > // Correct!
class CLASS
{
};

I does help the specify your compiler since some compilers have their own rules about default template arguments.

Wazzak
Hi Framework,
Thanks for answering this question.
This is the compiler which I use.
gcc version 4.4.3
Best,
Vikram
closed account (zb0S216C)
I can't seem to find the manual for your compiler. Sorry. However, you could try what I previously posted and see if that fixes your problem.

Wazzak
Thanks, your fix works for me.
Topic archived. No new replies allowed.