Xref: utzoo comp.lang.c++:8589 comp.unix.i386:7158 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Newsgroups: comp.lang.c++,comp.unix.i386 Subject: Re: stream manipulators which take arguments: HELP! Keywords: stream manipulators Message-ID: <45657@brunix.UUCP> Date: 24 Jul 90 16:28:11 GMT References: <331@ndla.UUCP> Sender: news@brunix.UUCP Reply-To: sdm@cs.brown.edu (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 45 In article <331@ndla.UUCP> platt@ndla.UUCP (Daniel E. Platt) writes: >I'd like some information on how to write stream manipulators which >takes arguments. All of the documentation I currently have on C++ 2.0 >doesn't cover it (I can't really call it 'documentation,' but its what >I have at the moment). From an application I have: From the .h file: // ---------------------------------------------------------------------------- // We need to create a manipulator taking a const int argument. Because no // such manipulator exists by default, we have to instantiate one using the // IOMANIP macros. These macros only understand simple typenames, so we have // to create a typedef for const int. I figured out how to do this by // looking at the lengthdemo example that came with the CC 2.0 distribution. // // indent is an ostream manipulator that adds whitespace to s. The amount // of whitespace is proportional to level. indent is designed to format // hierarchical structures. // ---------------------------------------------------------------------------- typedef const int ConstInt; // create simple typename IOMANIPdeclare(ConstInt); // magic macro call #1 OMANIP(ConstInt) indent(const int level); // magic macro call #2 ostream& indent(ostream& s, const int level = 0); // this is the actual manip. function From the .C file: const LEVEL_INDENT = 3; // number of spaces to indent per level when // printing out hierarchical information ostream& indent(ostream& s, const int level) { for (int i = level * LEVEL_INDENT; i > 0; i--) s << " "; return s; } OMANIP(ConstInt) indent(const int level) { return OMANIP(ConstInt)(indent,level); } Scott