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 6236977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:56:40+00:00 2026-05-24T10:56:40+00:00

I’ve got a quasi-singleton class (quasi-singleton in that most use refers to a single

  • 0

I’ve got a quasi-singleton class (quasi-singleton in that most use refers to a single object that is a function static, but users can also construct their own local copy for short-term use) that I would like to have write to cout from its destructor, and wanted to know if cout was guaranteed to be available during the static deinitialization phase following program termination. From this question it seems that the answer is yes (function static initialized objects should have their destructors called in reverse order from when they were constructed, which should be after cout was set up), but I wanted to check.

// Count calls to a logging function from some point in our code, to determine
// how many times it gets executed during a run, then report calls at the end
// of the program.  A quick-and-dirty way of determining how often we execute
// code.
class callCounter;
class callCounter {
public:
    callCounter() : has_calls_(false), counts_() {}
    ~callCounter () {report(std::cout);}
    void logCall(const std::string callSite);
    void report(std::ostream &os);
    void reset();
    static callCounter *theCounter();
private:
    typedef std::map<std::string, callCount> COUNTMAP;
    bool has_calls_;
    COUNTMAP counts_;
};

callCounter *callCounter::theCounter()
{
    static callCounter theCounts;
    return &theCounts;
}

Typical usage would be:

callCounter::theCounter()->logCall("foo");

So is this usage of cout in the destructor guaranteed safe (at least as far as using cout itself is concerned – arguably to be perfectly safe, I should wrap the report in a try block in case the write throws an exception.)?

  • 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-24T10:56:41+00:00Added an answer on May 24, 2026 at 10:56 am

    Short Answer: Yes

    Long Answer: Yes

    There are a couple of tricks that the standard library uses to make sure that std::cout (and std::cin/std:cerr) are available before any other objects (But you must #include <iostream> in the translation unit). Because they are created first (the reverse order of destruction rule guarantees) they are destroyed after your objects.

    See: n3242 (Practically the C++0x standard Pub: 2011-02-28)

    27.4 Standard iostream objects

    Paragraph 2

    The objects are constructed and the associations are established at some time prior to or during the first time an object of class ios_base::Init is constructed, and in any case before the body of main begins execution. The objects are not destroyed during program execution. The results of including in a translation unit shall be as if <iostream> defined an instance of ios_base::Init with static storage duration. Similarly, the entire program shall behave as if there were at least one instance of ios_base::Init with static storage duration.

    Added highlighting.

    From n1804 (which is basically the 2003 standard: Pub: 2005-04-27)

    27.3 Standard iostream objects

    Paragraph 2

    Mixing operations on corresponding wide- and narrow-character streams follows the same semantics as mixing such operations on FILEs, as specified in Amendment 1 of the ISO C standard. The objects are constructed, and the associations are established at some time prior to or during first time an object of class ios_base::Init is constructed, and in any case before the body of main begins execution. The objects are not destroyed during program execution280.

    Unfortunately the bit that would make it a guarantee is in the footnotes (which are non-normative (ie its not guaranteed just what the implementer should try and achieve).

    With the footnotes:

    280) Constructors and destructors for static objects can access these objects to read input from stdin or write output to stdout or stderr.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.