|
Boost : |
From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-10-24 07:05:14
Here's a class template that keeps track of its instantiations:
#include "boost/static_assert.hpp"
#include "boost/config.hpp"
typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];
template< typename U > no_tag test(U*, ...);
template< typename T, long > struct instantiation_count;
template<
typename T
, long value_ =
sizeof(test(static_cast<instantiation_count<T,1>*>(0)))
+ sizeof(test(static_cast<instantiation_count<T,2>*>(0)))
+ sizeof(test(static_cast<instantiation_count<T,3>*>(0)))
+ sizeof(test(static_cast<instantiation_count<T,4>*>(0)))
+ sizeof(test(static_cast<instantiation_count<T,5>*>(0)))
>
struct instantiation_count
{
BOOST_STATIC_CONSTANT(long, value = value_ - 5 + 1);
friend yes_tag test(instantiation_count<T, value>*);
};
// here!
BOOST_STATIC_ASSERT(instantiation_count<int>::value == 1);
BOOST_STATIC_ASSERT(instantiation_count<int>::value == 2);
BOOST_STATIC_ASSERT(instantiation_count<int>::value == 3);
BOOST_STATIC_ASSERT(instantiation_count<int>::value == 4);
BOOST_STATIC_ASSERT(instantiation_count<int>::value == 5);
BOOST_STATIC_ASSERT(instantiation_count<int>::value == 6);
BOOST_STATIC_ASSERT(instantiation_count<long>::value == 1);
BOOST_STATIC_ASSERT(instantiation_count<short>::value == 1);
BOOST_STATIC_ASSERT(instantiation_count<long>::value == 2);
Seems well-formed to me, and works with Intel C++ 6.0 and one other
compiler.
Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk