typename vs class (Visual Studio 2010)


Under what circumstances does NOT ‘class’ replace ‘typename’?

struct C
{
class CC{};
typedef CC CC2;
};
template<class T> //using 'class' is fine
struct D
{
typename T::CC2 c1; //'typename' cannot be prelaced by 'class'
class T::CC c2; //using 'class' is fine
//T::CC c3; //error: 'T::CC' : dependent name is not a type
//class T::CC2 c4; //error C2242: typedef name cannot follow class/struct/union
//T::CC2 c5; //error: 'T::CC2' : dependent name is not a type
};
D<C> a;

Summary: if the subtype if not typedef-ed, class can do the job of typename.
Otherwise, it has to be typename.

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 
{
public:
	enum {MAX = 2}; //only hold two elements
protected:
    T maData[MAX];
public:
    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;
public:
	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)	
}

std::forward vs static_cast


For perfect forwarding, I am still trying to give an use case in which std::forward<T> works but static_cast<T&&> doesn’t.
It was suggested just to use static_cast<T&&> to implement std::forward<T>, but it had problems.
I’d like to pin point the problem with┬áthe simplest snippet.
It’s very much appreciated if you have a solution to share with me.

template calculates 4 + 3 + 2 + 1


This is a “hello world” for template metaprogramming.
MetaFunc() return 10 because (4 + 3 + 2 + 1) equals 10.

template<int n>
int MetaFunc()
{
	return n +  MetaFunc<n-1>();
}
template<>
int MetaFunc<1>()
{
	return 1;
}
int TestMetaFunc()
{
	return MetaFunc<4>(); //4 + 3 + 2 + 1 == 10
}