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 implementation of std bind2nd from scratch

This home-made Bind2nd, which simulates std::bind2nd, is described as following:
1. make a regular two-parameter functor and add some typedefS, which will be used by the adapter.
2. make a one-parameter functor based one the typedefS.
3. make the adapter, which returns a one-parameter functor.

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; //these typedef are for adapter Bind2nd
	typedef int first_argument_type;
	typedef int second_argument_type;
template<typename T>
struct FunctorWithOneParameter //this is what Bind2nd transforms FunctorAdd to
	T mt;
	typename T::second_argument_type mr; //'typename' cannot be prelaced by 'class'
	FunctorWithOneParameter(const T& t, typename T::second_argument_type r): mt(t), mr(r) { }
	typename T::result_type operator()(typename const T::first_argument_type& l) { return mt(l, mr); }
template<typename T>
FunctorWithOneParameter<T> Bind2nd(const T& t, typename T::second_argument_type r) //Bind2nd is the home-made adapter
	return FunctorWithOneParameter<T>(t, r);

void TestAdapter()
{	int iBind = Bind2nd(FunctorAdd(), 3)(1); //bind by home-made Bind2nd
	_ASSERT(iBind == 4);
	int a[] = {0, 2, 3 };
	int c = count_if ( a, a + 3, Bind2nd(FunctorAdd(), 0) );//count the ones that are not 0
	_ASSERT(c == 2); //this proves Bind2nd is a successful adapter for FunctorAdd

Find out which allocators the DirectShow filters are using?

In my DVR media player projects, I made two filters: one source filter(push) to read data from DVR, and one decoder (transform) filter to decode the video. Most allocator work is taken care of by DirectShow base classes. I dig deeper into the allocators when I was doing renderless mode.
The question is: How many allocators are in the graph and where are they from?
There are two, one from decoder’s input pin, and one from VMR’s input pin.
Here are my findings by stepping into the base class code.
First allocator (used by source filter):

CBaseOutputPin::DecideAllocator(IMemInputPin *pPin, __deref_out IMemAllocator **ppAlloc)
//*ppAlloc is the allocator the source filter's output pin will be using.
//*ppAlloc is from decoder's input pin. 
//It's created (by CreateMemoryAllocator, instead of re-used) beause decoding isn't inplace
//instead of using allocator from next input pin, this pin can override InitAllocator to use it's own.
//*ppAlloc is apporved by DecideBufferSize()

Second allocator (used by decoder filter):

CBaseOutputPin::DecideAllocator(IMemInputPin *pPin, __deref_out IMemAllocator **ppAlloc)
//*ppAlloc is the allocator the decoder filter's output pin will be using.
//*ppAlloc is from VMR's input pin.
//*ppAlloc is apporved by DecideBufferSize()

Summary: for a non-inplace transform filter, in most circumstances, it’s input pin provides allocator for it’s upstream output pin, and it’s outpin uses the allocator from it’s downstream’s input pin.

Why EXACTLY does the compiler need help from dllimport?

extern "C" __declspec(dllimport) void __stdcall func(); //func() is in a DLL file and called by an executable

Many articles say that dllimport makes more efficient code by bypassing a jump.
But why is there a jump at the first place?
Short answer: because there is not enough room for the linker to fill in with a function pointer.

Long answer:
the compiler doesn’t know whether func() is from dll or not, and it assumes it’s not(to save space and time). The compiler leaves a slot at the spot where func() is called for the linker to fill in with func()’s address. If it’s a non-dll function, no problem. If it IS a dll function, the linker doesn’t know the function’s address; it only knows the pointer pointing to this address(IAT entry). Here is the linker’s solution: using a stub.
The linker fills the slot with the address of the stub. The stub’s code has JMP to jump to where the function pointer points to(in IAT).
This explains why stub(jump) is needed. Of course if the compiler knows it’s a dll function, it doesn’t need the stub, because the compiler can simply leave the slot big enough for the linker to fill it with the function pointer(and ‘DWORD PTR’, but ignore it to keep it simple).