我有以下代碼...調用專門的ostream操作
#include <sstream>
enum class eTag
{
A,
B,
C
};
template<eTag I> std::ostream& operator<< (std::ostream& str, int i)
{
return str; // do nothing
}
template<> std::ostream& operator<< <eTag::A>(std::ostream& str, int i)
{
return str << "A:" << i; // specialize for eTag::A
}
template<> std::ostream& operator<< <eTag::B>(std::ostream& str, int i)
{
return str << "B:" << i; // specialize for eTag::B
}
template<> std::ostream& operator<< <eTag::C>(std::ostream& str, int i)
{
return str << "C:" << i; // specialize for eTag::C
}
int main()
{
std::ostringstream s;
// s << <eTag::A>(42) << std::endl;
return 0;
}
這編譯。但是從main()的註釋行中可以看到,我正在爲如何實際調用ostream操作符的專門化而苦惱。
可能重複的是它不可能手動調用C++操作符?](http://stackoverflow.com/questions/7225962/is-it-not-possible-to-call-c-operators-manually) – Pradhan 2015-03-30 21:22:44
雖然可怕,但運營商<<(std :: cout,42)<< std :: endl;'。我更好奇*爲什麼*你想這樣做。 ' –
WhozCraig
2015-03-30 21:24:09
@Pradhan不是真的很愚蠢,對吧?正如你所關聯的問題主要是關於爲基本類型重載'operator +'。 – vsoftco 2015-03-30 21:30:46