How to define a template specialization for a friend function or operator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to define a template specialization for a friend function or operator?

I wish to overload a friend function or a friend operator in a different way for a data type. How can I achieve the same ?

13th May 2017, 12:05 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
33 Answers
+ 9
need to be able to bookmark posts on the phone. i know i will need to do this one day
13th May 2017, 2:35 PM
jay
jay - avatar
+ 9
Nice Idea. And whilst I am here.. Kinshuk: Well done! I had faith in you the whole time!
14th May 2017, 12:06 AM
jay
jay - avatar
13th May 2017, 12:17 PM
jay
jay - avatar
+ 8
p.s when you find out? can you share a code example with comments? cause you know, you are awesome!
13th May 2017, 12:23 PM
jay
jay - avatar
+ 7
@‎ɐısıօլɐ It is finally done! I finally completed it! This is what I used: // Forward declare the class template <class M> class Matrix; // Forward declare the template operator template <typename M> ostream& operator<<(ostream& out, const Matrix<M>& matrix); // Forward declare the function ostream& operator<<(ostream& out, const Matrix<Complex>& mc); //Now the Matrix Class : template<class M> class Matrix { //Many useful Functions... friend std::ostream& operator<<(std::ostream& out, const Matrix<Complex>& matrix ) //Function Specialization { for(int i=0;i<mc.r;i++) for(int j=0;j<mc.c;j++) { cout<<"Element at "<<i<<","<<j<<" - "<<mc.mat[i][j]<<endl; } } friend std::ostream& operator<< <>(std::ostream& out, const Matrix<M>& matrix ) //Original Template Overload... { for(int i=0;i<matrix.r;i++) for(int j=0;j<matrix.c;j++) cout<<matrix.mat[i][j]; } } Thanks a Ton for your help!
13th May 2017, 2:33 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
Yes, as anyone would prefer << over a print function in C++ atleast...
13th May 2017, 1:56 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
I really forgot the & in the specialization 😅. But on putting it, i am left with just the second error I mentioned before...
13th May 2017, 1:58 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
So one must force declare the class and its function for specializations... Yes, it must be made as an instance...
13th May 2017, 2:34 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
:) aloisia is smart too! but you never gave up either
14th May 2017, 11:07 AM
jay
jay - avatar
+ 5
Yes, making a print function and overloading works, but only if its not a friend... I guess Ill have to adjust with the print function for this task...
13th May 2017, 1:53 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
well the first error says you should've written template<class T> before the operator<< declaration, I don't know if that'll work though if it is inside the Matrix class
13th May 2017, 2:01 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 5
Why is that required though when there is template<class M> before the class?
13th May 2017, 2:02 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
or I just saw a solution on stack overflow that looks like this, maybe it'll work for you? friend ostream& operator<< <T>(ostream& o, const Matrix<T>& mat)
13th May 2017, 2:04 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 5
Ill try it then...
13th May 2017, 2:04 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
that's required because it is not a method of the class Matrix but just a friend function and you can't specialize the function over the class Matrix, because it is not an instance of it
13th May 2017, 2:05 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 5
It didn't work... Perhaps I am not able to implement it as said...
13th May 2017, 2:09 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
Amazing, I'm glad I could help you :D Cheers! 🍻
13th May 2017, 2:35 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 5
@jay maybe share the link to the discussion to yourself and copy it into a memo :D
13th May 2017, 2:37 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 4
@jay Sorry, but that didn't help as its just a detailed explanation for operator overloading... I want to know how I can overload the friend operators or functions of a template class for a data type like double or char...
13th May 2017, 12:53 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
@jay Sorry, but that didn't help... I wish to know a way so I can overload a friend function of a template class for a particular data type like double or char.
13th May 2017, 12:54 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar