views:

95

answers:

2

I have a class that is defined as the following:

template <class WidgetType>
class CometWidget : public WidgetType;

Inside a function I am doing this:

dynamic_cast<CometWidget *>(iter2->second.second)->changesCommited_();

and it resolves the CometWidget type, complies and run correctly.
The code runs inside the CometWidget class.
How on earth does this happen?
Why is that so? Should it even compile?

A: 

Very interesting indeed. It seems to me like an interesting compiler bug.

It is possible to deduce the correct argument of the CometWidget<> template - just the same way you can deduce template parameters of a function from argument list. If it would be static cast, it would be less surprising.

With dynamic cast, there's little expected to be in common between the source and the target type. So, such "guessing" might have occurred, but then it's not a rightful one.

What compiler is this?

Pavel Radzivilovsky
It is VC++ 2008. See MSN's comment. Is he right?
the_drow
@the_drow yes MSN is right. The "CometWidget" is called the "injected class name". If you write it from within the template, then it's equivalent to the name with the parameter list followed and names a type, not the template.
Johannes Schaub - litb
litb - add this as an answer and I'll accept it :)
the_drow
+2  A: 

If it's inside the declaration of CometWidget then you don't need to explicitly qualify the template (or whatever term you use to say CometWidget<...>).

MSN
Could you show me the part of the standard that says so?
the_drow
http://stackoverflow.com/questions/1263825/is-this-valid-c-code/1264004#1264004
MSN