View Poll Results: Do C++ output streams need a better interface?

Voters
3. You may not vote on this poll
  • Definitely

    0 0%
  • Doesn't even matter

    1 33.33%
  • Absolutely not

    2 66.67%
Multiple Choice Poll.

Thread: Do C++ output streams need a better interface?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277

    Do C++ output streams need a better interface?

    Do you ever get the feeling when you're working with std ostreams that there's just way too much typing going on? I do. Never quite understood why the left-shift operator was chosen either.

    So thinking about it for a bit and wondering just how much time and effort it would take to put a better output interface together. Started coding and then something like twelve lines of code later, I'm done!

    Code:
    /* osprint.hpp */
    
    #include <iostream>
    
    struct osprint
    {
     std::ostream& stream;
    
     osprint()
     : stream(std::cout)
     { }
    
     osprint(std::ostream& stream)
     : stream(stream)
     { }
    
     template <typename V>
     osprint& operator = (const V& rhs)
     {
      stream << rhs; 
      return *this;
     }
    };
    
    template <typename V>
    inline osprint& operator , (osprint& lhs, const V& rhs)
    {
     return lhs = rhs; 
    }
    
    #include <string>
    
    const std::string eol = "\n";
    
    /* Usage */
    
    #include <cmath>
    
    using namespace std;
    
    osprint out;
    
    int main()
    {
     out = "Euler's number is ", exp(double(1)), eol;
    }
    If that isn't a testament to the power of C++, I don't know what is!

    Anyway, I just like the interface better. Plus it's about half as much typing. Kind of doesn't work for those who use commas a lot to separate statements of course. But who really does that anyway?
    Last edited by Sir Galahad; 12-03-2019 at 06:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2019, 11:32 AM
  2. input/output streams
    By cybernike in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 11:15 PM
  3. Input/Output Streams
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 02:58 PM
  4. output file streams
    By neilman88 in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2003, 07:11 PM
  5. Checking for errors on output streams
    By Heraclitus in forum C Programming
    Replies: 1
    Last Post: 03-27-2003, 03:47 PM

Tags for this Thread