DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
G2++ Tutorial - G2++(C++)

Stream Errors

It is the responsibility of the user-defined extractor to assign a null value of the appropriate type to its second argument if it cannot construct an object of that type from the characters in the input stream. Doing this properly may require extra error-handling code for some user-defined types, but the overhead is necessary if G2++ is to be as robust in handling user-defined types as it is in handling builtin types.

The code for extracting Time in the section, ``Providing Inserters and Extractors for User-Defined Types'', in this topic, did not include error handling:

           istream& operator>>(istream& is,Time& t){
               long x;
               is >> x;             // this could fail
               t=make_time(x);
               return is;
           }

There are two things that can go wrong here: (1) the external representation may contain non-numeric characters or (2) the external representation may have more digits than a long can represent. Either error will cause is to test as null after the extraction, causing subsequent extractions to have no effect. We can compensate for this undesired behavior as follows:

           istream& operator>>(istream& is,Time& t){
               long x;
               if(is>>x){
                   t=make_time(x);
               }else{
                   is.clear();
                   t=Time::MIN;
               }
               return is;
           }

One Time value will be lost, but the input stream will still be readable, allowing the typed extractor to re-synchronize itself on the next newline. See the manual entries for class ios(C++) for more information on stream errors and how to detect and handle them.


Next topic: Adding Builtin C Types to the Repertoire of G2++ Types
Previous topic: Pointers

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005