A functor from scratch that works with std bind2nd

For a functor to work with bind2nd, it usually derives from binary_function.
However, making one from scratch is not much work as illustrated in the following.

struct FunctorAdd //takes two parameters, operator() returns the sum of the two.
int operator()(const int & i, const int & j) const 
{ return i+j; 
typedef int result_type;
typedef int first_argument_type;
typedef int second_argument_type;
void Test_bind2nd()
int a[] = {0, 2, 3 };
int c = count_if ( a, a + 3, bind2nd(FunctorAdd(), 0) ); //bind2nd is a system adapter in std
//count the ones that are not 0
_ASSERT(c == 2); //this proves FunctorAdd works well with bind2nd

Find out the length of array

void TestLengthArray()
	enum {E_ARRAY_LEN = 2};
	int *a = new int[E_ARRAY_LEN];
	size_t iSize = _msize(a) / sizeof a[0];
	_ASSERT(iSize == E_ARRAY_LEN); //here we got the length of the array

MSDN didn’t mention if _msize can be used for ‘new’; it just seems working.
This is not supported by the standard, but good to know it’s possible at runtime.

dynamic_cast requires virtual functions (polymorphic), why?

First of all, if you ALREADY know the cast is safe, static_cast can replace dynamic_cast, even in mutilple inheritance, except virtual inheritance.
Please see this post for why.

struct D: virtual B {...};//have to use dynamic_cast to cast B* to D*

I have to go through a few steps to explain this.
1. dynamic_cast needs access to RTTI (again, the above link says why).
2. RTTI is only accessable through vtable pointer.
3. no virtual functions, no vtable pointer. (RTTI can sit right above vtable)
4. Conclusion, dynamic_cast needs virtual function.

A minimum iterator template that works with ostream_iterator

This iterator works with std::ostream_iterator to get the container’s content printed to cout.
It’s minimum because it stops working if I take one more element out.
Minimum snippets is best to show how things work under the hood.

template <class T> class CMyContainer 
	enum {MAX = 2}; //only hold two elements
    T maData[MAX];
    CMyContainer<T>(const T& t1, const T& t2) 
		maData[0] = t1; maData[1] = t2; //pre-initialized to only 2 element
    class iterator;
    iterator begin() 
		return iterator(0, this); 
    iterator end() 
		return iterator(MAX, this); 
template <class T> class CMyContainer<T>::iterator
    CMyContainer<T> * mpContainer;
    int miIndex;
	typedef std::forward_iterator_tag iterator_category;
	typedef void value_type;
	typedef void difference_type;
	typedef void distance_type;
	typedef void pointer;
	typedef void reference;
    iterator () {}
    iterator (int iIndex, CMyContainer<T> * c): miIndex(iIndex), mpContainer(c) {}
    bool operator==(const iterator & z) 
		return miIndex == z.miIndex; 
    bool operator!=(const iterator & z) 
		return !operator==(z);
    void operator++() 
		++ miIndex;
    T& operator*()
		return mpContainer->maData[miIndex]; 

void TestCustomIterator()
	CMyContainer<int> b(1, 2);
	CMyContainer<int>::iterator it;
	ostream_iterator<int> o(cout, " ");
	copy(b.begin(), b.end(), o); //this line prints 1 2 (content of b)