Connecting Tech Pros Worldwide Forums | Help | Site Map

Template overloading

KK
Guest
 
Posts: n/a
#1: Dec 1 '05
Hello all,
I'm sure this must be a simple matter of language syntax, but could
not find an appropriate reference so I'm posting the question here.
Consider

template < class T = double >
class Example
{
T _val;
//define constructor & destructor
void Init ( int a );
}

template <class T > //do this if T is of type double, int, unsigned
void Init (int a)
{
_val = T(a);
}

so far it is fine ... I would like to overload function 'Init' when the
'T' is type 'float' with something like

template <class T >
void InitFloat (int a)
{
_val = T (a+1);
}

how can I achieve both functions exist with same function name?

Thank you
-KK


Ron Natalie
Guest
 
Posts: n/a
#2: Dec 1 '05

re: Template overloading


KK wrote:
KK wrote:[color=blue]
> Hello all,
> I'm sure this must be a simple matter of language syntax, but could
> not find an appropriate reference so I'm posting the question here.
> Consider
>
> template < class T = double >
> class Example
> {
> T _val;
> //define constructor & destructor
> void Init ( int a );
> }
>
> template <class T > //do this if T is of type double, int, unsigned
> void Init (int a)
> {
> _val = T(a);
> }
>
> so far it is fine ... I would like to overload function 'Init' when the
> 'T' is type 'float' with something like
>
> template <class T >
> void InitFloat (int a)
> {
> _val = T (a+1);
> }
>[/color]
template <> void Init<Float>(int a) { ...
roberts.noah@gmail.com
Guest
 
Posts: n/a
#3: Dec 1 '05

re: Template overloading



KK wrote:[color=blue]
> Hello all,
> I'm sure this must be a simple matter of language syntax, but could
> not find an appropriate reference so I'm posting the question here.
> Consider
>
> template < class T = double >
> class Example
> {
> T _val;
> //define constructor & destructor
> void Init ( int a );
> }
>
> template <class T > //do this if T is of type double, int, unsigned
> void Init (int a)
> {
> _val = T(a);
> }
>
> so far it is fine ... I would like to overload function 'Init' when the
> 'T' is type 'float' with something like
>[/color]
edit:
[color=blue]
> template <>
> void Init<float> (int a)
> {
> _val = T (a+1);
> }[/color]

hgupta@bearwagner.com
Guest
 
Posts: n/a
#4: Dec 1 '05

re: Template overloading


try this.

Hitendra

template < class T = double >
class Example
{
T _val;
//define constructor & destructor
public:
void Init ( int a );
};

template <class T > //do this if T is of type double, int, unsigned
void Example<T>::Init (int a)
{
cout << "Init for non float" << endl;
_val = T(a);
}

void Example<float>::Init (int a)
{
cout << "Init for float" << endl;
_val = T (a+1);
}

hgupta@bearwagner.com
Guest
 
Posts: n/a
#5: Dec 1 '05

re: Template overloading


try this.

Hitendra

template < class T = double >
class Example
{
T _val;
//define constructor & destructor
public:
void Init ( int a );
};

template <class T > //do this if T is of type double, int, unsigned
void Example<T>::Init (int a)
{
cout << "Init for non float" << endl;
_val = T(a);
}

void Example<float>::Init (int a)
{
cout << "Init for float" << endl;
_val = T (a+1);
}

Victor Bazarov
Guest
 
Posts: n/a
#6: Dec 1 '05

re: Template overloading


KK wrote:[color=blue]
> Hello all,
> I'm sure this must be a simple matter of language syntax, but could
> not find an appropriate reference so I'm posting the question here.
> Consider
>
> template < class T = double >
> class Example
> {
> T _val;
> //define constructor & destructor
> void Init ( int a );
> }
>
> template <class T > //do this if T is of type double, int, unsigned
> void Init (int a)[/color]

void Example<T>::Init(int a)
[color=blue]
> {
> _val = T(a);
> }
>
> so far it is fine ... I would like to overload function 'Init' when the
> 'T' is type 'float' with something like
>
> template <class T >
> void InitFloat (int a)[/color]

void Example<T>::InitFloat(int a)
[color=blue]
> {
> _val = T (a+1);
> }
>
> how can I achieve both functions exist with same function name?[/color]

What you do is _specialise_:

template<>
void Example<float>::Init(int a)
{
_val = a+1;
}

V
Closed Thread