• Advertisement
Sign in to follow this  

Template function in template class

This topic is 4149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, What is the correct syntax for defining template functions that are declared in a template class? template<typename T> class A { template<typename C) void foo(); }; template<typename T, C> void A<T>::foo() {} //doesn't work

Share this post


Link to post
Share on other sites
Advertisement

template<typename T>
template<typename C> // under each other
void A<T>::foo()
{

}

Share this post


Link to post
Share on other sites
EDIT: Got the wrong end of the stick with this one, but interesting none the less.
To call your foo function:

A <typename> a;
a.foo <typename> ();

If the function was declared static:

template <class T>
class A
{
public:
template <class S> static void foo (void);
};

A <typename>::foo <typename> ();

Skizz

Share this post


Link to post
Share on other sites
It depends on what ur doing, but this should work:




template< class BarType >
class Bar
{
public:

template< class FooType >
void Foo( FooType i_Whatever )
{
BarElem = static_cast< BarType >( Whatever );
}

// This is a specialization of the above method.
template<>
void Foo( double i_Double )
{
BarElem = static_cast< BarType >( Double );
}

private:
BarType m_BarElem;
};

void main ( void )
{
Bar< int > myBar;

// will result in call to general template
myBar.Foo( 5.0f );

// will result in call to specialized double template method
myBar.Foo( 5.0 );

// force a call to a specific version
// will convert 42.0f to integer before calling
myBar.Foo< int >( 42.0f );
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement