#include <stlsoft/util/unused_return_value_monitor.hpp>
V | The value type. This is the type that is to be returned by the function whose return type is to be monitored | |
M | The monitor function type. If the return value is not used, the monitor function instance will be called. | |
R | The reference type. This is the reference type, used in the constructor of the monitor, and the type of the member variable storing the value |
struct int_monitor { public: void operator ()(void const* instance, int value) const { printf("Unused return value %d from object instance %p\n", value, instance); } }; class X { public: unused_return_value_monitor<int, int_monitor> fn() { return 10; // A copy is taken, but copies of ints usually cost the same as references to ints } };
Where the return type is a complex type, you can make efficiency savings by using a (C++) reference for the reference type. However, this must only be done when the returned value will persist for the duration of the calling expression.
struct string_monitor { public: void operator ()(void const* instance, string const& value) const { printf("Unused return value %s from object instance %p\n", value.c_str(), instance); } }; class Y1 { public: Y2() : m_str("What's new, Pussycat?") {} public: // Must store return value by value, since the returned value will not persist for the duration of the calling expression unused_return_value_monitor<string, string_monitor> fn_by_value() { return "Hello sailor"; } // Must store return value by (const) reference, since the returned value will persist for the duration of the calling expression unused_return_value_monitor<string, string_monitor, string const&> fn_by_ref() { return m_str; } private: string const m_str; };
Public Types | |
typedef V | value_type |
typedef M | monitor_function |
typedef R | reference_type |
typedef unused_return_value_monitor< V, M, R > | class_type |
Public Member Functions | |
Construction | |
unused_return_value_monitor (reference_type value) | |
unused_return_value_monitor (reference_type value, monitor_function monitorFn) | |
unused_return_value_monitor (class_type const &rhs) | |
~unused_return_value_monitor () throw () | |
Destructor. | |
Conversion | |
operator value_type () const |
~unused_return_value_monitor | ( | ) | throw () [inline] |
Destructor.
This requires that unused_return_value_monitor is *never* used inside a catch block.