Originally Posted by
laserlight
No, it is not as simple as that because the function call does not involve the istream. One way out is for the function call to create an object that has operator>> overloaded for basic_istream. This object then contains the argument provided as an argument to the input manipulator, and then its operator>> uses that value to perform the input manipulation.
After simplifying a little and abstracting the work of the >> in the () operator, here is my try:
Code:
template<typename T>
class literal
{
T t;
public:
literal(T t_):t(t_){};
istream& operator()(istream& is)
{
T temp;
is>>temp;
if(temp!=t)is.setstate(ios_base::failbit);
return is;
}
};
template<typename T>
istream& operator>>(istream& is,literal<T> lt)
{
return lt(is);
}
Example use:
Code:
int main()
{
int x,y;
cin>>x>>literal<char>('+')>>y;
cout<<x<<endl<<y;
return 0;
}
Doe this fall short anywhere ?