STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

unused_return_value_monitor Class Template Reference

Return value adaptor for monitoring whether return values are used. More...

#include <stlsoft_unused_return_value_monitor.h>

List of all members.


Detailed Description

template<typename V, typename M, typename R = V>
class stlsoft::unused_return_value_monitor< V, M, R >

Return value adaptor for monitoring whether return values are used.

Parameters:
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
If the value type is a simple type, you can just allow the reference type R to be defaulted, as in:

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;
};


The documentation for this class was generated from the following file:

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004