Проблема не в кортеже, а в вашем операторе.Это прекрасно работает:
ostream& operator<<(ostream& os_, Mytuple tuple_)
{
os_ << tuple_.get<0>(); // Error because int is implicitly converted into Mytuple. WHYY?
os_ << tuple_.get<1>(); // No Clue Why this is ambiguous.
//os_ << tuple_.get<2>(); // Error because no matching operator. Fine.
return os_;
}
Проблема в том, что ostringstream наследует operator<<
от ostream, который имеет эту подпись: ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)
разрешено.Затем
ostream& operator<<(ostream& os, T t)
(замените T всеми доступными типами в c ++, см. Оператор << ссылочная страница </a>
EDIT
Вот упрощенный пример (без кортежа):
ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)
{
const int i = tuple_.get<0>();
os_ << i; // error in this line
return os_;
}
и ошибка теперь:
dfg.cpp: In function ‘std::ostringstream& operator<<(std::ostringstream&, Mytuple)’:
dfg.cpp:18: error: 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/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/bits/ostream.tcc:111: note: candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
dfg.cpp:14: note: candidate 2: std::ostringstream& operator<<(std::ostringstream&, Mytuple)
В приведенном выше сообщении об ошибке говорится: невозможно выбирать междудва оператора operator<<(ostream&,...)
и operator<<(ostringstream&,...). This also raises another question : why on earth do you need
operator << (ostringstream &, ...) `? </p>