Template function in template class

Started by
3 comments, last by snooty 17 years, 6 months ago
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
Advertisement
template<typename T>template<typename C>  // under each othervoid A<T>::foo(){}
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
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 );}

Why did Doug Gregor create the empty directory?
Thanks a lot.

This topic is closed to new replies.

Advertisement