|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - Funky template bug - a trimmed down example
Walter, as promised, here's a trimmed down example (an extract from COMSTL).
It's still not tiny, but is self-contained and as small as can be. This code
works with Borland (5.5, 5.6), Intel (6 & 7), VC++ (5, 6, 7), G++ (2.95 &
3.2), Metrowerks CodeWarrior (7 & 8).
#include <windows.h>
#include <algorithm>
#include <functional>
#ifndef __DMC__
using std::unary_function;
using std::for_each;
#endif /* __DMC__ */
template<typename R, typename T>
class std_mem_fun_t
: public unary_function<T*, R>
{
public:
typedef R (STDAPICALLTYPE T::*method_type)();
public:
explicit std_mem_fun_t(method_type pmfn)
: m_f(pmfn)
{}
R operator ()(T *pt) const
{
return (pt->*m_f)();
}
private:
method_type m_f;
};
template <class R, class T>
inline std_mem_fun_t<R, T> std_mem_fun(R (STDAPICALLTYPE T::*f)())
{
return std_mem_fun_t<R, T>(f);
}
int main()
{
// Dimension of the array
const int C_UNKNOWNS = 10;
// This is the CLSID for the GIT, though it could be anything really
static const CLSID _CLSID_WidelyAvailableStdObject =
{
0x00000323
, 0x0000
, 0x0000
, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }
};
::CoInitialize(NULL);
LPUNKNOWN unknowns[C_UNKNOWNS];
// Create them
for(int i = 0; i < C_UNKNOWNS; ++i)
{
if(FAILED(::CoCreateInstance( _CLSID_WidelyAvailableStdObject,
NULL,
CLSCTX_ALL,
IID_IUnknown,
reinterpret_cast<void**>(&unknowns[i]))))
{
unknowns[i] = NULL;
}
}
LPUNKNOWN *begin = unknowns;
LPUNKNOWN *end = begin + C_UNKNOWNS;
// Doesn't work for temporaries here ...
for_each(begin, end, std_mem_fun(&IUnknown::Release));
// ... or in explicit form either
std_mem_fun_t<ULONG, IUnknown> fn(&IUnknown::Release);
::CoUninitialize();
return 0;
}
May 20 2003
May 20 2003
|