Home arrow C++ arrow Page 3 - Custom Stream Manipulation in C++

Custom Stream Manipulation in C++

The creators of the STL library have surely gone beyond the expectations of many programmers. However, as perfect and complete as their library may seem at first, you will slowly discover that even they could not cover everything. That's when it is time for you to learn how to create a custom class/function that suits your specific needs.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
May 05, 2009
  1. · Custom Stream Manipulation in C++
  2. · Manipulators with No Argument
  3. · Solution for Multiple Arguments
  4. · Templates on the Fly

print this article

Custom Stream Manipulation in C++ - Solution for Multiple Arguments
(Page 3 of 4 )

This is all fine; I applied three manipulators with the creation of a new one, or transformed the reset of the float flags into a manipulator -- but even with this, I had to call the setprecision explicitly to set the precision. In a perfect scenario, I could do this with just the scientify(3) call.

To figure out how we can do this, let us investigate how to make this the developing team inside the STL library. How can the example on the previous page work? It works because inside the iostream library there is, defined for the insertion operator, the following:

basic_ostream& operator<<

(basic_ostream& (__clrcall *_Pfn)( basic_ostream&));

//where-> event_callback _Pfn; // pointer to event handler

Inside this, the function just takes a basic_ostream reference and returns the same reference; it simply takes a function and passes it further. Nevertheless, if you plan to pass some parameters inside the function as well, there is a problem. How can you pass them toward the function so that you will know the stream has them also? The obvious answer is to include the stream into its parameters.

Now if I start doing so I end up with something like this:

cin << streamify(3, cin);

This is a redundant solution and not elegant under any circumstances; we can find a better solution. The main power of the manipulators is to add it with the << operator to the function. We must not break that convention!

The solution is to trick the compiler. In addition, for this we need to use all of the overloading capabilities of C++. First, create a class that will do the work. For this we declare a local variable holding the input number and an overload for the () operator. The basic constructor will naturally exist inside, where we need to initialize the data.

class tempForScience


tempForScience (int n) { m_v = n };

void operator() (ostream& stream) const

{stream << scientify << setprecision(m_v); }; // here we can // whatever we want on the stream, for instance apply more // manipulators on it

int m_v;


This class will do the work for us, but we must initialize a type like this during the stream insertion, so we write a function that will just return a creation from this:

tempForScience setScientify(int n)


return tempForScience(n);


Now we called the function we want, but the problem that we still have is that the insertion operator will not know what to do with the tempForScience() object we just returned. We just need to take another step to resolve this issue.

ostream& operator<<(ostream& str, const tempForScience & tFS) {

tFS (str); // Pass the stream to the tfs object

// to do iterate back and accomplish all

return(str); // return the stream so we can insert // objects further in the stream


Insert all we learned for now after the scientify manipulator in the earlier code and change the call lines for the cout to the following:

cout << setScientify(3) << i << endl;

cout << resetFloat << i << endl;

The result is what we were aiming for, hurray!



Press any key to continue . . .

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials