Y
YUY0x7
Hi, I am having a bit of trouble with a specialization of operator<<.
Here goes:
class MyStream
{
};
template <typename T>
MyStream& operator<<(MyStream& lhs, T const &)
{
cout << "operator<< T" << endl;
return lhs;
}
// specialize to work differently for std::string
template <> MyStream& operator<<(MyStream& lhs, string const &)
{
cout << "operator<< const string&" << endl;
return lhs;
}
// specialize to work differently for char const*
template <> MyStream& operator<<(MyStream& lhs, char const * const &)
{
cout << "operator<< char const*" << endl;
return lhs;
}
int main()
{
MyStream strm;
int i = 19;
string mystr = "hello";
strm << "hi" << i << mystr << "\n";
return 0;
}
This outputs:
operator<< T
operator<< T
operator<< const string&
operator<< T
Why doesn't strm << "hi" (and << "\n") use the specialized version?
Now I tried changing the operator to these:
template <typename T>
MyStream& operator<<(MyStream& lhs, T)
{
cout << "operator<< T" << endl;
return lhs;
}
// specialize to work differently for std::string
template <> MyStream& operator<<(MyStream& lhs, string const &)
{
cout << "operator<< const string&" << endl;
return lhs;
}
// specialize to work differently for char const*
template <> MyStream& operator<<(MyStream& lhs, char const *)
{
cout << "operator<< char const*" << endl;
return lhs;
}
And the output is:
operator<< char const*
operator<< char const*
operator<< T
operator<< char const*
Now why doesn't strm << mystr use the specialized one? It looks like
it's a perfect match for mystr (not even using a specialization for
string& works).
Can anyone tell me why this happens, and if there is a way to make this
work for both char const* and string const& ?
Thanks a lot,
George Faraj
Here goes:
class MyStream
{
};
template <typename T>
MyStream& operator<<(MyStream& lhs, T const &)
{
cout << "operator<< T" << endl;
return lhs;
}
// specialize to work differently for std::string
template <> MyStream& operator<<(MyStream& lhs, string const &)
{
cout << "operator<< const string&" << endl;
return lhs;
}
// specialize to work differently for char const*
template <> MyStream& operator<<(MyStream& lhs, char const * const &)
{
cout << "operator<< char const*" << endl;
return lhs;
}
int main()
{
MyStream strm;
int i = 19;
string mystr = "hello";
strm << "hi" << i << mystr << "\n";
return 0;
}
This outputs:
operator<< T
operator<< T
operator<< const string&
operator<< T
Why doesn't strm << "hi" (and << "\n") use the specialized version?
Now I tried changing the operator to these:
template <typename T>
MyStream& operator<<(MyStream& lhs, T)
{
cout << "operator<< T" << endl;
return lhs;
}
// specialize to work differently for std::string
template <> MyStream& operator<<(MyStream& lhs, string const &)
{
cout << "operator<< const string&" << endl;
return lhs;
}
// specialize to work differently for char const*
template <> MyStream& operator<<(MyStream& lhs, char const *)
{
cout << "operator<< char const*" << endl;
return lhs;
}
And the output is:
operator<< char const*
operator<< char const*
operator<< T
operator<< char const*
Now why doesn't strm << mystr use the specialized one? It looks like
it's a perfect match for mystr (not even using a specialization for
string& works).
Can anyone tell me why this happens, and if there is a way to make this
work for both char const* and string const& ?
Thanks a lot,
George Faraj