The proper form for a member function template would be:

    template <class T>
    void ObjectStream::WriteObject<T> ( const T & obj ) { /* ... */ }


with the T after the function name as well as before the function header.
WriteObject cannot be a virtual function. Judging by the fact that your
compiler is letting you declare it inside the class body would lead me to
believe that this should work, but I give you no guarantees. The deal is
that member function templates are allowed *now*, but they were illegal in a
previous version of the C++ standard. As a result, whether or not your
compiler will let you use them is completely dependent on when it was made.
Unfortunately,  I don't know about your version of Metroworks. Good luck; if
you find that you can't do it take solace in the fact that many other before
you have run up against this as well. :)

-Jaison Lee


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Scott L.
Johnson
Sent: Tuesday, February 29, 2000 4:38 PM
To: Palm Developer Forum
Subject: [C++] CW doesn't support member function templates outside class
body?

(Using NT 4.0, CW R6, compiler version 2.3.1, __MWERKS__ == 0x2301)

I'm writing a stream class.  It includes a member function template to
generically support writing out objects of any type.  It looks like this
(simplified for illustration) --

    struct ObjectStream
    {
        template <class T>
        void WriteObject( const T & obj ) { /* ...do stuff... */ }
    };

This works, PROVIDED that the member template body is defined inside the
class declaration.  But I want to move it down outside the class body, for
readability and compliance with our coding standards.  I believe the syntax
outside the class is supposed to be something like this:

    template <class T>
    void ObjectStream::WriteObject( const T & obj ) { /* ... */ }

But this doesn't work -- the CW compiler complains about an illegally
defined template.  (I've tried it with the "<T>" part omitted and in
different places, but nothing works.)  I believe this _is_ supposed to be
legal C++, though my truckload of C++ books fails me at the moment.

So, does anybody know if the CodeWarrior compiler is supposed to support
this, or not support it, or if I'm making some other error here?

-slj-


(BTW, I'm not looking for help writing a stream class or any other
implementation advice.  Just worrying about the specific C++
language/compiler issue presented above.)


--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palm.com/devzone/mailinglists.html


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to