Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5989165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:03:46+00:00 2026-05-22T23:03:46+00:00

Possible Duplicate: Implementing a no-op std::ostream Is there any stream equivalent of NULL in

  • 0

Possible Duplicate:
Implementing a no-op std::ostream

Is there any stream equivalent of NULL in c++? I want to write a function that takes in a stream if the user wants to have the internal outputted to somewhere, but if not, the output goes into some fake place

void data(std::stream & stream = fake_stream){
    stream << "DATA" ;
}

i want to be able to chose to do data() or data(std::cout)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:03:47+00:00Added an answer on May 22, 2026 at 11:03 pm

    Edit: Taken from @Johannes Schaub – litb’s mail here with slight modifications:

    template<typename Ch, typename Traits = std::char_traits<Ch> >
    struct basic_nullbuf : std::basic_streambuf<Ch, Traits> {
         typedef std::basic_streambuf<Ch, Traits> base_type;
         typedef typename base_type::int_type int_type;
         typedef typename base_type::traits_type traits_type;
    
         virtual int_type overflow(int_type c) {
             return traits_type::not_eof(c);
         }
    };
    
    // convenient typedefs
    typedef basic_nullbuf<char> nullbuf;
    typedef basic_nullbuf<wchar_t> wnullbuf;
    
    // buffers and streams
    // in some .h
    extern std::ostream cnull;
    extern std::wostream wcnull;
    
    // in a concrete .cpp
    nullbuf null_obj;
    wnullbuf wnull_obj;
    std::ostream cnull(&null_obj);
    std::wostream wcnull(&wnull_obj);
    

    Use those:

    void data(std::ostream& stream = cnull){
      // whatever...
    }
    

    Now, this looks cool and all, but the following is way shorter and works, because if a null pointer is provided to the constructor of ostream, it automatically sets the badbit and silently ignores any writes:

    // in .h
    extern std::ostream cnull;
    extern std::wostream wcnull;
    
    // in .cpp
    std::ostream cnull(0);
    std::wostream wcnull(0);
    

    The standard guarantees this works, beginning from 27.6.2.2 [lib.ostream.cons] p1 which describes the constructor of ostream that takes a pointer to a streambuf:

    Effects: Constructs an object of class basic_ostream, assigning initial values to the base class by calling basic_ios<charT,traits>::init(sb).

    The relevant function from basic_ios, 27.4.4.1 [lib.basic.ios.cons] p3:

    void init(basic_streambuf<charT,traits>* sb);
    Postconditions: The postconditions of this function are indicated in Table 89:

    The important row from Table 89:

    rdstate() — goodbit if sb is not a null pointer, otherwise badbit.

    What happens if the badbit is set is described under 27.6.2.6 [lib.ostream.unformatted]:

    Each unformatted output function begins execution by constructing an object of class sentry. If this object returns true, while converting to a value of type bool, the function endeavors to generate the requested output.

    This implies that, in case the sentry is false, it does not. Here is how the sentry converts to bool, taken from 27.6.2.3 [lib.ostream::sentry] p3 & p5:

    3) If, after any preparation is completed, os.good() is true, ok_ == true otherwise, ok_ == false.

    5) operator bool();
    Effects: Returns ok_.

    (ok_ is a member of ostream::sentry of type bool.)


    Note that these quotes are still present in C++11, just in different places. In order of appearance in this answer:

    • 27.6.2.2 [lib.ostream.cons] p1 => 27.7.3.2 [ostream.cons] p1
    • 27.4.4.1 [lib.basic.ios.cons] p3 => 27.5.5.2 [basic.ios.cons]
    • Table 89 => Table 128
    • 27.6.2.6 [lib.ostream.unformatted] => 27.7.3.7 [ostream.unformatted] p1
    • 27.6.2.3 [lib.ostream::sentry] p3 & p5 => 27.7.3.4 [ostream::sentry] p4 & p5
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: UIScrollView. Any thoughts on implementing “infinite” scroll/zoom? I notice that in the
Possible Duplicate: UIScrollView. Any thoughts on implementing “infinite” scroll/zoom? I've got an UIScrollView and
Possible Duplicate: UIScrollView. Any thoughts on implementing “infinite” scroll/zoom? I have a UIScroll which
Possible Duplicate: Why there is no multiple inheritance in Java, but implementing multiple interfaces
Possible Duplicate: Ruby: Any gems for threadpooling? Is there a better ruby lib thread
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: StructureMap singleton usage (A class implementing two interface) I'm currently designing a
Possible Duplicate: Interface vs Base class A class implementing an interface has to implement
Possible Duplicate: Forgot Password: what is the best method of implementing a forgot password
Possible Duplicate: How do you remove Subversion control for a folder? I am implementing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.