Function template as member in a class template 
Author Message
 Function template as member in a class template

Hi all.

Is it possible to define a member function template outside
a class template?
This sample code compiles and runs, but the FuncB() is inline.
How, if possible, should i define the function outside the class?

#include <iostream.h>
#include <typeinfo.h>

template<class TYPE>
class Ctest
{
public:
    void FuncA( TYPE );
    template<class OTHER>
    void FuncB( TYPE var_a, OTHER var_b )
    {
        cout << var_a << " is of type "
            << typeid( var_a ).name() << endl;
        cout << var_b << " is of type "
            << typeid( var_b ).name() << endl;
    }

Quote:
};

template<class TYPE>
void Ctest<TYPE>::FuncA( TYPE var_a )
{
    cout << var_a << " is of type "
        << typeid( var_a ).name() << endl;

Quote:
}

// I want to define FuncB here, but HOW?

int main()
{
    Ctest<char *> test;
    test.FuncA( "Hello" );
    test.FuncB( "world", 1 );

     return 0;

Quote:
}

TIA
Niklas
--
To reply, remove NOSPAM form my address.


Thu, 28 Jun 2001 03:00:00 GMT  
 Function template as member in a class template
Quote:

>Hi all.

Hi.

Quote:
>Is it possible to define a member function template outside
>a class template?

What you refer to is called a "member template"
in the C++ standard.

The C++ language permits it.  But VC6 does not.
One of the few compiler enhancements in VC6 was
to make member templates work, provided they are
defined inline, in the class definition.

Quote:
>This sample code compiles and runs, but the FuncB() is inline.
>How, if possible, should i define the function outside the class?

>#include <iostream.h>
>>#include <typeinfo.h>

For new code, you should be using
  #include <iostream>
  #include <typeinfo>

Quote:
>template<class TYPE>
>class Ctest
>{
>public:
>    void FuncA( TYPE );
>    template<class OTHER>
>    void FuncB( TYPE var_a, OTHER var_b )

      ; // body snipped

Quote:
>};

>// I want to define FuncB here, but HOW?

template<class TYPE> template<class OTHER>
void Ctest<TYPE>::FuncB( TYPE var_a, OTHER var_b )
{
    cout << var_a << " is of type "
        << typeid( var_a ).name() << endl;
    cout << var_b << " is of type "
        << typeid( var_b ).name() << endl;

Quote:
}

But don't try this at home with VC6 or lower.
This syntax is according to the C++ standard,
not Microsoft's approximation of C++.

--Larry Brasfield
Above opinions may be mine alone.



Thu, 28 Jun 2001 03:00:00 GMT  
 Function template as member in a class template

Quote:

>Is it possible to define a member function template outside
>a class template?

Yes, but as described in the VC6 ReadMe, VC6 doesn't yet support this.
In VC6, member templates must be defined within the class body.

Quote:
>How, if possible, should i define the function outside the class?

Here are a couple of examples to show what it would look like:

struct A
{
   template<class T> void fun(T);

Quote:
};

template<class T>
void
A::fun(T)
{

Quote:
}

template<class T>
struct B
{
   template<class T2> void fun(T2);

Quote:
}

template<class T> template<class T2>
void
B<T>::fun(T2)
{

Quote:
}

--
Doug Harrison



Thu, 28 Jun 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. template class with a template member function

2. Template member functions of template classes

3. calling template member function in template class

4. template member function - non template class - VC5 - error

5. Simple question: Member function templates in template classes under VC++ 5.0

6. Using member function templates in template class under VC++ 5.0

7. calling template member function in template class

8. template functions in template classes declared outside the class definition

9. Template function within Template Class

10. pb with template function in non template class

11. template function in a template class

12. Template function in non-template class

 

 
Powered by phpBB® Forum Software