I want to specialize operator<< but this code is not compiling;
template<>
std::ostream& operator<< < my_type >( std::ostream& strm, my_type obj);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To specialize a template, first you have to have a template declared.
In the case of a free
operator<<you don’t need a template; you can just overload it for yourmy_typeclass:If your object isn’t trivial in size, you may want to consider passing via a const reference so that you don’t copy it every time you stream it:
(Technically you can explicitly specialize an
operator<<, but I don’t think that this is what you want or need. In order to be able to use a template operator<< with the usual << syntax you need to make the template specialization deducible from one of the parameter types.E.g.
)