It does not matter. But I'm curious when this warning appears. My real question is why ostream and stream are handled differently.
struct Test { int y; Test(int k) : y(k) {} };
With this simple structure, the compiler sees that int
can be converted to Test
.
Therefore, I get a warning with this code:
std :: ofstream& operator<< (std :: ofstream& os, const Test& t) { os << ty; return os; }
When he sees os << ty
, he does not know if I want to press int called ty, or I want to convert int to Test first and then click it. This seems rather strange, you would think that he would prefer the unprocessed overload of int ofstream& operator<< (ofstream &os, int)
.
g ++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3:
template_expl.cpp: In function 'std::ofstream& operator<<(std::ofstream&, const Test&)': template_expl.cpp:15: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: /usr/include/c++/4.4/bits/ostream.tcc:105: note: candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>] template_expl.cpp:13: note: candidate 2: std::ofstream& operator<<(std::ofstream&, const Test&)
In any case, one way to solve this problem is to mark the constructor in Test as explicit
. I can live with it. But it is strange that if ofstream
is replaced by ostream
, then the warning goes away. Any idea why?
Aaron mcdaid
source share