easythrees
easythrees

Reputation: 1650

compilation error with template functions inline

I have a simple template function that uses two parameters:

template<typename To, typename From>To* asSomething( Common *item)
{
    From  * tdnItem = downcast( item, From );
    To    * someClass = NULL;

    if( tdnItem != NULL ) 
    {
        someClass = downcast( tdnItem->gloo(), To );
    }

    return someClass;
}

Later, I call this helper method in another inline method:

return asSomething<ToFoo, FromFoo>(item);

However, I get a weird compilation error in Visual Studio:

error C2065: 'FromTmeta' : undeclared identifier
see reference to function template instantiation 'To *asSomething<ToFoo,FromFoo>(Common * *)' being compiled
with
[
    To=ToFoo
]
error C2065: 'ToTmeta' : undeclared identifier

The function declaration looks correct to me, what's the issue here?

Upvotes: 1

Views: 137

Answers (1)

R Sahu
R Sahu

Reputation: 206697

I suspect downcast is a pre-processor macro. Pre-processor macros and template parameters don't work well if you are using the arguments to concatenate other things to the type.

Replace

From  * tdnItem = downcast( item, From );

by

From  * tdnItem = dynamic_cast<From*>(item);

and

someClass = downcast( tdnItem->gloo(), To );

by

someClass = dynamic_cast<To*>( tdnItem->gloo());

Upvotes: 2

Related Questions