|
|
|
|
|
|
|
|
|
|
|
|||||||
#include <stlsoft_unused_return_value_monitor.h>
| 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;
};
|
|
| STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004 |