Default arguements in Template

N

Nike

I have a small question w.r.t usage of default arguements in
template..
I shall try to elaborate this with an example..
let's say I have some template function , where EntryType is the input
for the template fn 1.. and another type where EntryType and lass P1
are both inputs..
case1 (1)template<class EntryType_>
i.e void A<EntryType_>::createObject(EntryType::inputIdType
inpId_ )..

case2 (2)EntryType and class P1 are the input for the template fn 1
template<class EntryType_, class P1>
i.e void A<EntryType_>::createObject(EntryType::inputIdType
inpId_ )..
Inside this function, I call the class constructor based on the number
of input parameters passed ..
I have similar 25 functions(Function overloading) where the difference
is going to be only in the number of arguements passed as input..(This
function is part of the base project)..
Now , for a new reqmt., I need to add a boolean to the create object
function..
It looks tedious for me to add the boolean in all the 25 template
functions as I feel the functionality is going to be the same in all
the 25 functions..
Ideally, what I would prefer is to have one single function with all
the parameters inside it..If I don't pass a parameter , let say in
case 2 of the above declaration , i should call case1
Normally, without the template this is possible with the usage of
default arguements as avbl in c++.
But for template this is not possible as the template function will
not know what kind of objects this is instantiating..
 
N

Nike

If my explanation looks complicated, I can give u a small code
sample..
#include <iostream.h>
template <class A , class B>
void print(A a ,B b){ //Default arguement is
not possible
cout<<a<<endl;
cout<<b<<endl;
}
void print1(int a , int b=0){
cout<<a<<endl;
cout<<b<<endl;
};
int main()
{

print<int>(3,4);
//print<int>(3); --->This will throw an error as template will not
know the type
print1(3,4);
print1(3);
return 0;
}

How to have default arguements in templates???
I have ggiven an example of 2 arguements..But actually , I have 25
arguements..
 
S

Sylvester Hesp

Nike said:
If my explanation looks complicated, I can give u a small code
sample..
#include <iostream.h>
template <class A , class B>
void print(A a ,B b){ //Default arguement is
not possible
cout<<a<<endl;
cout<<b<<endl;
}
void print1(int a , int b=0){
cout<<a<<endl;
cout<<b<<endl;
};
int main()
{

print<int>(3,4);
//print<int>(3); --->This will throw an error as template will not
know the type
print1(3,4);
print1(3);
return 0;
}

How to have default arguements in templates???
I have ggiven an example of 2 arguements..But actually , I have 25
arguements..

There is a proposal to allow default template arguments for function
templates in the new standard, but until then I guess you'd have to use
regular function overloading as a work-around

// general function
template<class A, class B, class C> void func(A a, B b, C c)
{
// ...
}

// overloads to simulate default arguments:
template<class A, class B> void func(A a, B b)
{
func(a, b, 23);
}

template<class A> void func(A a)
{
func(a, "hi there", 23);
}

- Sylvester
 

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,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top