[cfe-dev] Typeid and forward declaration - Clarification needed

Karen Shaeffer shaeffer at neuralscape.com
Tue Oct 29 11:23:57 PDT 2013


On Tue, Oct 29, 2013 at 01:59:56PM +0530, Rahul Jain wrote:
> Hi all,
> 
> This is with respect to the following TC.
> 
> struct S;
> 
> template <typename T>
> static bool fn (S *s)
> {
>   return typeid (*s) == typeid (T);
> }
> 
> struct S
> {
> };
> 
> bool x = fn<S> (__null);
> 
> 
> The standard says:
> If the type of the type-id is a class type or a reference to a class type,
> the class shall be completely-defined.
> 
> 
> This "shall" is kind of confusing. Is this a valid TC. Is forward
> declaration allowed while using the typeid operator?
> 
> Any clarification on the same would be appreciated.
> 
> Thanks,
> Rahul
--- end quoted text ---

Hi Rahul,
That test case does work on g++-4.8.1. The reason is that fn is a template function.
And the compiler does not instantiate it until it is needed. That is down at the call
site, 'bool x = fn<S>(__null);', where the full definition of struct S is in scope.


If you refactor fn to be a plain function, then you will get the error due to use
of an incomplete type 'struct S'.

enjoy,
Karen
-- 
Karen Shaeffer                 Be aware: If you see an obstacle in your path,
Neuralscape Services           that obstacle is your path.        Zen proverb



More information about the cfe-dev mailing list