他の回答が示すように、標準に準拠していません。
意図した使用法を示していないため、正確に何を求めているのかを理解するのは少し難しいですが、コードを自分で実装できます (以下を参照)。これは、特定のクラスごとに変換テンプレートを作成することを避けるために、SFINAE でさらに改善できます。
#include <iostream>
#include <sstream>
using namespace std;
struct A
{
int x;
A() : x(0) {}
};
istream& operator>>(istream& in, A& a)
{
in >> a.x;
return in;
}
ostream& operator<<(ostream& on, A& a) { return on << "A: " << a.x; }
struct B
{
int x;
B(istream& in) : x(0) { in >> x; }
};
ostream& operator<<(ostream& on, B& b) { return on << "B: " << b.x; }
struct C
{
int x;
C() : x(0) {}
static C OfStreamX(istream& in)
{
C c;
in >> c.x;
return c;
}
};
ostream& operator<<(ostream& on, C& c) { return on << "C: " << c.x; }
template <typename T> T Read(istream& in);
template <> A Read(istream& in)
{
A a;
in >> a;
return a;
}
template <> B Read(istream& in) { return B(in); }
template <> C Read(istream& in) { return C::OfStreamX(in); }
int main()
{
string data("23 45 67");
istringstream in(data);
A a = Read<A>(in);
cout << a << endl;
B b = Read<B>(in);
cout << b << endl;
C c = Read<C>(in);
cout << c << endl;
}
出力:
A: 23
B: 45
C: 67