Sie sind auf Seite 1von 3

New User?

Register

Sign In

Help

Make Y! My Homepage

Mail

My Y!

Yahoo!

Search

Search Web

HOME

BROWSE CATEGORIES

MY ACTIVITY

ABOUT

Ask

What would you like to ask? Continue

Answer
Search Y! Answers

Discover

What are you looking for?

Advanced Search

Home > All Categories > Computers & Internet > Programming & Design > Resolved Question

Resolved Question
gh0st Rider

Show me another

Ready to Participate? Get Started!

Need help understanding this in [C++]?


Here is the article i was reading : http://www.cantrip.org/traits.html "What happens when we parameterize streambuf on the character type? We need not only a type for the character, but for the type of the EOF value. Here's a start: template <class charT, class intT> class basic_streambuf { ... intT sgetc(); int sgetn(charT*, int N); }; The extra template parameter clutters things up. Users of iostream don't care what the end-of-file mark is, or its type, and shouldn't need to care. Worse, what value should sgetc() return at end-of-file? Must this be another template parameter? The effort is getting out of hand. " Q1 : why extra template parameter **clutters** things up? Q2 : "what value should sgetc() return at end-of-file? Must this be another template parameter?" My solution : template <class charT> class basic_streambuf { ... int sgetc(); // <- return type changed return the next character, or EOF. ( from article , very first code, since whether we instantiate it with chat or wchar_t, when ever EOF occurs we simple return the special EOF value, doesn't it solve the problem? int sgetn(charT*, int N); };

Categories
All Categories Computers & Internet Computer Networking Hardware Internet Other - Computers Programming & Design Security Software
SPONSOR RESULTS

Mercedes Benz R Class


Planning to buy a new car. Find best choice cars at CarWale. CarWale.com/Mercedes-Benz-RClass See your message here...

If(my_above_solution==false) Q3: then please tell me what is the problem there or problem domain with example. Thanks a lot, I really appreciate the help)
3 weeks ago Report Abuse

Best Answer - Chosen by Voters


Cubbi Q1: the author answers that in the next sentence. The users of iostream would have to use basic_streambuf<char, int> (and, therefore, basic_istream<char, int>). Granted this is not a very big deal because most people use the library typedefs (istream, wostream, etc) anyway. Q2: The end-of-file indicator is different for every char type. In my gcc: EOF is -1 of type int WEOF is UINT_MAX of type unsigned int char_traits<char16_t>::eof() is USHRT_MAX of type unsigned short char_traits<char32_t>::eof() is the same as WEOF On my Visual Studio 2010, EOF is -1 of type int WEOF is USHRT_MAX of type unsigned short char_traits<char16_t>::eof() is the same as WEOF char_traits<char32_t>::eof() is -1 of type long int If your hypothetical int basic_streambuf<charT>::sgetc() returns some EOF of type int regardless of what type is CharT, it may happen to be a valid character given another CharT.
3 weeks ago Report Abuse

67% 2 Votes
2 people rated this as good

Action Bar:

Interesting!

Email

Comment (0)

Save

Other Answers (1)


Q1. It really doesn't, aside from the fact that the author appears to feel that it's annoying to have to define these extra types and values in the template to handle a single special case. I see his point, but if the alternatives are that, or no parameterized streambuf type at all, or a streambuf type that isn't compatible with the standard definition of a streambuf, then adding the extra information to the template is still the least bad option. Q2. The problem with your solution is that sgetc() needs to be able to return a character of whatever type is defined to be charT. If charT is a very wide type (e.g., unsigned 32-bit value), then int (if it's a signed 32-bit value) will be unable to represent all possible values of charT. Further, if charT is a signed character of any length and can return valie character values of -128 through 127, or -32768 through 32767, or whatever, then what int should be returned to represent EOF? It can't be the standard EOF value of -1, because that may be a valid signed character in the character set being used. Q3. If the user must specify in the template not only the charT type, but also the intT type and the specific intT value associated with EOF, then the user can ensure that an appropriate integer type is selected that fully contains all the possible values of the character type, and the EOF value can be chosen as some value that can never be returned from the stream as an actual valid character.
3 weeks ago Report Abuse

Chris

33% 1 Vote
2 people rated this as good

Discover Questions in Programming & Design


l l l l

PHP help loop please? Positioning things on webpage in CSS? Positioning things on my webpage with CSS? Changing font color of list in CSS?

Answers International Argentina Italy Spain Australia Japan Taiwan Brazil Canada Mexico China New Zealand France Philippines Germany Quebec Vietnam Hong Kong Singapore enEspaol India Indonesia

Malaysia Thailand

South Korea

United Kingdom

United States

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer. Help us improve Yahoo! Answers. Send Feedback Copyright 2011 Yahoo! Inc. All Rights Reserved. Copyright/IP Policy - Privacy Policy - About Our Ads - Terms of Service - Community Guidelines - Safety Tips

Das könnte Ihnen auch gefallen