1

I am using gcc 4.2.4. According to Schildt, "C++ The Complete Reference", 1995, user defined parameterized manipulators should be defined as:

istream &mymanip(istream &stream, type param)
{
    // here my code 
    return stream;
}
// overload 
imanip<type> mymanip(type param){
    return imanip<type>(mymanip, param)
}
// usage
cin >> mymanip(param);

I believe that imanip(mymanip, param) is supposed to return an object that contains mymanip and mymanip's arguments. This should be used by an overloaded operator>> to call mymanip. However, this does not work, imanip is not declared.

I also found this version:

IMANIP(int) fld(int n){
    return IMANIP(int)(fld,n);
    }

which also does not work.

My questions are:

  1. does gcc 4.2.4 follow the ANSI C++ standard on this detail or not? If it does, did the standard change since 1995 in this respect?
  2. in order to define my own parameterized manipulators do I need to understand the iostream code and write my own overloaded imanip and operator>> functions?
  3. regardless the answer to question 2, what is the best strategy for me to understand the iostream code? Should I read the code? How do I find out the names of compiled libraries which are part of iostream? Should I read a book?
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Doru Georgescu
  • 309
  • 2
  • 12
  • What error are you getting ? – iammilind Nov 26 '12 at 10:56
  • 2
    Did you ever came across the term "bullschildt" ? – PlasmaHH Nov 26 '12 at 10:59
  • @iammilind: error: ‘imanip’ was not declared in this scope. On the first line which uses imanip. I included `#include #include `. – Doru Georgescu Nov 26 '12 at 11:04
  • I found some libraries searching for the package which contains the iomanip header and then grepping into the files in the package. – Doru Georgescu Nov 26 '12 at 11:22
  • @PlasmaHH You are right, the book is a very poor manual, it just follows the standard instead of explaining. I believe that it also misses things, like for example that [inherited functions are not overloaded](http://stackoverflow.com/questions/13631124/no-match-for-operator-for-stdendl-after-overload). – Doru Georgescu Dec 01 '12 at 08:13
  • Your problem is reading something by Schildt. In the future, don't waste your money on Bullschildt. – Puppy Dec 01 '12 at 11:22

1 Answers1

2

The code you posted seems out of context. imanip looks like some template, but that template is never defined anywhere in your code.

But to be honest, I would not even consider using a 1995 book on C++ any more. In 1995 C++ was not even standardized, although there existed an "annotated reference manual". Your book is over 17 years old, which means "more than extremely outdated" when it comes to programming languages. C++ has evolved a lot since then, you should consider to buy a more recent book, perhaps take a look at http://isocpp.org/get-started to give you some examples.

To give you more specific answers to your questions:

  1. Yes, gcc follows the standard in this regard, but your book might not, because it is pre-standard.
  2. Manipulators are made so you do not have to reimplement the op<< and op>> again. However, depending on what your manipulators are designed to do, you might need to call some of the lower level methods provided by the stream or streambuf.
  3. "The code" does not exist, there are several implementors of standard libraries, and they all have their own, sometimes very complicated code for etc. I would recommend to search the web for information about how those libraries work. Of course, a book is even better. Consider buying "The C++ Standard Library" by N. Josuttis. There is a recent Edition that covers the standard library "as of now and tomorrow", i.e. including the additions of the C++11 standard.
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • Thank you for your answer. This is precisely what I was looking for. Since the Internet is empty, probably user defined parameterized manipulators are no longer part of the standard. I suppose that they met some practical problems related to their implementation. – Doru Georgescu Nov 26 '12 at 12:48
  • 1
    Wrong! I finally searched for user defined parameterized manipulators, not just for imanip parameterized manipulators and I found them alive and well: http://www.angelikalanger.com/Articles/C++Report/Manipulators1.pdf, http://programming-technique.blogspot.ro/2011/11/c-tutorial-user-defined-manipulators.html. – Doru Georgescu Nov 26 '12 at 12:58