”check” function – OK to throw in destructor? My goal is to provide a function with the following functionality:

// Will throw iff a != 42.
check(a == 42) &lt;&lt; a &lt;&lt; " is not equal to 42!";  


I have the following:

class check
{
public:
    check(bool everything_OK = false)
        : should_throw{!everything_OK}
    { }

    check(const check&amp;) = delete;

    ~check() noexcept(false)
    {
        if (should_throw &amp;&amp; !uncaught_exception()) {
            throw runtime_error(stream.str());
        }
    }

    template&lt;typename T&gt;
    check&amp; operator &lt;&lt; (T&amp;&amp; t)
    {
        if (!should_throw) {
            return *this;
        }

        stream &lt;&lt; std::forward&lt;T&gt;(t);
        return *this;
    }

private:
    const bool should_throw;
    stringstream stream;
};


The thing is, all guidelines I have read forbid throwing in the destructor. I have also read experts saying they do not know of a good use of std:uncaught_exception. Can I still achieve what I want? 

ADDITION:
Loki’s point below can be fixed by defining a macro:

#define my_check(expr) (expr) ? ((void)0) : check(false)


But once we allow ourselves to use macros, there are other solutions provided below.
Ignoring the throwing out of the destructor (which is a bad idea).

My main problem is with usage:

check(a == 42) &lt;&lt; a &lt;&lt; " is not equal to 42!"; 


Here you present a trivial case. But not all error checking is trivial. I think a more realistic case (that will happen more often than you think);

Universe  a;
// .... STUFF
check(a.theMeaningLTUAE(42)) &lt;&lt; a &lt;&lt; " The truth not reached " &lt;&lt; a.checkInvariants();


Here we have calls to functions that will need to be evaluated even if they are never used (specifically checkInvariants() may be expensive (or even the streaming of a)). If the check passes you are still obliged to call it here but its value are simply discarded. I would prefer that the function is never called if no error message is going to be produced. 

What you really want is a short cut operator that prevents subsequent operations from happening. Note overloading &amp;&amp; and || on a class does not yield short-cut operators.

Or alternatively any functions should be passed as callbacks to operator&lt;&lt; and only evaluated if there output is required.