{"id":1880,"date":"2025-01-25T21:25:30","date_gmt":"2025-01-25T21:25:30","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1880"},"modified":"2025-01-26T17:50:03","modified_gmt":"2025-01-26T17:50:03","slug":"writing-your-own-stream-manipulators","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/i-o-streams\/writing-your-own-stream-manipulators\/","title":{"rendered":"Writing Your Own Stream Manipulators"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Writing_Your_Own_Stream_Manipulators\"><\/span>Writing Your Own Stream Manipulators<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We&#8217;ve seen several stream manipulators such as <strong><code>endl<\/code><\/strong>, <strong><code>setw(int)<\/code><\/strong>, etc.&nbsp; Maybe you would like to implement your own stream manipulators, or you don&#8217;t like the ones provided, and you&#8217;d like to produce a more consistent interface of your own\u2026<\/p>\n\n\n\n<p>There are two cases:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stream manipulators without arguments, and:<\/li>\n\n\n\n<li>Stream manipulators with arguments<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Stream_Manipulators_without_Arguments\"><\/span>Stream Manipulators without Arguments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This is much the simpler of the two cases.<\/p>\n\n\n\n<p>A stream manipulator without arguments is a function that accepts a reference to a stream, and returns that reference.<\/p>\n\n\n\n<p>For example, a stream manipulator that inserts a tab into an output stream might look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px;line-height:1.4\"><code><strong>ostream&amp; tab(ostream&amp; out)\n{\n\treturn out &lt;&lt; '\\t';\n}<\/strong>\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" style=\"font-size:clamp(16.293px, 1.018rem + ((1vw - 3.2px) * 0.68), 25px);\"><span class=\"ez-toc-section\" id=\"Questions\"><\/span><em>Questions:<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>What are the types of operands in the following expression?<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>cout &lt;&lt; tab<\/strong><\/code><\/pre>\n\n\n\n<p>What kind of <code><strong>operator &lt;&lt;<\/strong> <\/code>must be provided in order for this to work?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" style=\"font-size:clamp(16.293px, 1.018rem + ((1vw - 3.2px) * 0.68), 25px);\"><span class=\"ez-toc-section\" id=\"Answers\"><\/span><em>Answers:<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The types of the operands of the expression&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>cout &lt;&lt; tab<\/strong><\/code><\/pre>\n\n\n\n<p>are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ostream<\/strong><\/li>\n\n\n\n<li><strong>ostream &amp;(*)(ostream &amp;)<\/strong><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-medium-font-size\"><em>Huh? Come again?&nbsp; What&#8217;s that second one?<\/em><\/p>\n<\/blockquote>\n\n\n\n<p>Let&#8217;s write a <strong>typedef<\/strong> to make it a little clearer:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>typedef ostream &amp;(*Omanip)(ostream &amp;);<\/strong><\/code><\/pre>\n\n\n\n<p>So the <strong>operator &lt;&lt;<\/strong> we need is:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px;line-height:1.4\"><code><strong>ostream &amp;ostream::operator&lt;&lt;(Omanip m)<\/strong>\n<strong>{<\/strong>\n  <strong>return (*m)(*this);<\/strong>\n<strong>}<\/strong><\/code><\/pre>\n\n\n\n<p>Got that? This operator is supplied in the <strong><code>iostream<\/code><\/strong> header file, and allows the manipulator to act on itself&#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  tab.h\n\/\/  iostreams - Stream Manipulator functions\n\/\/\n\/\/  Created by Bryan Higgs on 9\/4\/24.\n\/\/\n\n#ifndef tab_h\n#define tab_h\n\n#include &lt;iostream&gt;\n\ninline std::ostream&amp; tab(std::ostream&amp; out)\n{\n  return out &lt;&lt; '\\t';\n}\n\n#endif \/* tab_h *\/\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  tab.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  iostreams - Stream Manipulator functions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 9\/4\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#ifndef<\/span><span style=\"color: #0000FF\"> tab_h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#define<\/span><span style=\"color: #0000FF\"> tab_h<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">inline<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">ostream<\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">tab<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">ostream<\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">out<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> out &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #EE0000\">\\t<\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#endif<\/span><span style=\"color: #008000\"> \/* tab_h *\/<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Here&#8217;s a simple program to test the tab Stream Manipulator function:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  iostreams - Stream Manipulator functions\n\/\/\n\/\/  Created by Bryan Higgs on 9\/4\/24.\n\/\/\n\n#include &lt;iostream&gt;\n\n#include &quot;tab.h&quot;\n\nint main(int argc, const char * argv[]) \n{\n  std::cout &lt;&lt; 'x' &lt;&lt; tab &lt;&lt; 'y' &lt;&lt; tab &lt;&lt; 'z' &lt;&lt; std::endl;\n  \n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  iostreams - Stream Manipulator functions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 9\/4\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;tab.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[]) <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;x&#39;<\/span><span style=\"color: #000000\"> &lt;&lt; tab &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;y&#39;<\/span><span style=\"color: #000000\"> &lt;&lt; tab &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;z&#39;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>and here&#8217;s what it outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>x\ty\tz\nProgram ended with exit code: 0<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Stream_Manipulators_with_Arguments\"><\/span>Stream Manipulators with Arguments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Writing stream manipulators with arguments is a much more advanced topic, beyond the scope of this coverage.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:30%\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"1000\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/09\/TheCProgrammingLanguage.jpg\" alt=\"\" class=\"wp-image-1317\" style=\"width:278px;height:auto\" srcset=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/09\/TheCProgrammingLanguage.jpg 801w, https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/09\/TheCProgrammingLanguage-240x300.jpg 240w, https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/09\/TheCProgrammingLanguage-768x959.jpg 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\">\n<p>If you want to learn how to do it, you can read Bjarne Stroustrup&#8217;s <em>&#8220;The C++ Programming Language<\/em>&#8220;, 4th Edition.&nbsp; <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Be warned that it&#8217;s not the most readable of books!<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/i-o-streams\/summary-of-i-o-streams\/\">Next: Summary of I\/O Streams<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Writing Your Own Stream Manipulators We&#8217;ve seen several stream manipulators such as endl, setw(int), etc.&nbsp; Maybe you would like to implement your own stream manipulators, or you don&#8217;t like the ones provided, and you&#8217;d like to produce a more consistent interface of your own\u2026 There are two cases: Stream Manipulators without Arguments This is much [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":936,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1880","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=1880"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1880\/revisions"}],"predecessor-version":[{"id":1897,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1880\/revisions\/1897"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/936"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=1880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}