FANDOM


Ignoring Function Return ValuesEdit

If it is important that a return value not be ignored by the caller of a function, the template dont_ignore may be used as follows:

BEFORE:

int add( int x, int y )
{
    return x + y;
}

AFTER:

dont_ignore<int> add( int x, int y )
{
    return x + y;
}

When the caller of the function does not use the return value, an exception is thrown. Definition of dont_ignore:

template<class T>
struct dont_ignore
{
    const T     v;
    bool        used;

    dont_ignore( const T& v )
        :  v( v ), used( false )
    {}

    ~dont_ignore()
    {
        if ( !used )
            throw std::runtime_error( "return value not used" );
    }

    operator T()
    {
        used = true;
        return v;
    }
};

This template is an example of Catching defects with Patterns.

Reference Edit

Stack Overflow: link

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.