Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Particularizing Template Method

Reply
Thread Tools

Particularizing Template Method

 
 
pmatos
Guest
Posts: n/a
 
      09-08-2005
Hi all,

Imagine I have: template <class T> class Matrix;
I can have a Matrix of doubles, of cars, trains, any object at all...
But know... I want to implement a method foo that should only work with
Matrix<double>. How can I specify this?

Cheers,

Paulo Matos

 
Reply With Quote
 
 
 
 
Someonekicked
Guest
Posts: n/a
 
      09-08-2005
template<class T>

class blah

{

public:

blah();

void foo(double x);

void moo(T x);

private:

T items;

};

template<class T>

blah<T>::blah()

{

}

void blah<double>::foo(double x)

{

cout << x << endl;

}

template<class T>

void blah<T>::moo(T x)

{

cout << x << endl;

}



The following will work

void main()

{

blah<double> y;

y.foo(23);

}

But this will give error



void main()

{

blah<char> y;

y.foo(23);

}




"pmatos" <> wrote in message
news: oups.com...
> Hi all,
>
> Imagine I have: template <class T> class Matrix;
> I can have a Matrix of doubles, of cars, trains, any object at all...
> But know... I want to implement a method foo that should only work with
> Matrix<double>. How can I specify this?
>
> Cheers,
>
> Paulo Matos
>



 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      09-08-2005
"Someonekicked" <> wrote in message
news:k72dnVqjWfdGrb3eRVn-
> template<class T>
>
> class blah
>
> {
>
> public:
>
> blah();
>
> void foo(double x);
>
> void moo(T x);
>
> private:
>
> T items;
>
> };
>
> template<class T>
>
> blah<T>::blah()
>
> {
>
> }
>
> void blah<double>::foo(double x)
> {
> cout << x << endl;
> }


The above function definition should be

template<>
void blah<double>::foo(double x)
{
cout << x << endl;
}

i.e., you should precede it with template<>


>
> template<class T>
>
> void blah<T>::moo(T x)
>
> {
>
> cout << x << endl;
>
> }
>
>
>
> The following will work
>
> void main()
>
> {
>
> blah<double> y;
>
> y.foo(23);
>
> }


main should return int --- and some compilers will insist on it.



--
John Carson
 
Reply With Quote
 
pmatos
Guest
Posts: n/a
 
      09-08-2005
Thanks all for the help... Workin'...

John Carson wrote:
> "Someonekicked" <> wrote in message
> news:k72dnVqjWfdGrb3eRVn-
> > template<class T>
> >
> > class blah
> >
> > {
> >
> > public:
> >
> > blah();
> >
> > void foo(double x);
> >
> > void moo(T x);
> >
> > private:
> >
> > T items;
> >
> > };
> >
> > template<class T>
> >
> > blah<T>::blah()
> >
> > {
> >
> > }
> >
> > void blah<double>::foo(double x)
> > {
> > cout << x << endl;
> > }

>
> The above function definition should be
>
> template<>
> void blah<double>::foo(double x)
> {
> cout << x << endl;
> }
>
> i.e., you should precede it with template<>
>
>
> >
> > template<class T>
> >
> > void blah<T>::moo(T x)
> >
> > {
> >
> > cout << x << endl;
> >
> > }
> >
> >
> >
> > The following will work
> >
> > void main()
> >
> > {
> >
> > blah<double> y;
> >
> > y.foo(23);
> >
> > }

>
> main should return int --- and some compilers will insist on it.
>
>
>
> --
> John Carson


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments