Topic: Syntax for member of nested class template


Author: arnstein@netcom.com (David Arnstein)
Date: Sat, 18 Sep 1993 18:28:44 GMT
Raw View
Consider the following source file nesplain.c (plain nesting).  It declares a
pair of nested classes Outer and Inner.  The Inner class has a member function
named operator= whose body is coded outside of both classes:

//////////  Begin nesplain.cc  \\\\\\\\\\
// Declaration of a pair of nested classes
class Outer
{
   class Inner
   {
      Inner operator= (Inner);
   };
};

// Delayed declaration of inner class method
Outer::Inner Outer::Inner::operator= (Inner arg)
{
   return arg;
}

// Verify that Outer class can be instantiated
void test ()
{
   Outer ou;
}
\\\\\\\\\\  End nesplain.cc  //////////

This compiles fine under Borland C++ 3.1 and g++ 2.3.3.  Now for the hard part.
I want to make the Outer class a class template.  I get compiler error messages
from Borland C++.  Apparently the version of g++ that I have doesn't understand
templates, so I couldn't test.  Here's the code that fails, I called the file
nestempl.cc (nested template):

//////////  Begin nestempl.cc  \\\\\\\\\\
// Declaration of a pair of nested classes
template<class T> class Outer
{
   class Inner
   {
      Inner operator= (Inner);
   };

   T whatever;
};

// Delayed declaration of inner class method
template<class T> Outer<T>::Inner Outer<T>::Inner::operator= (Inner arg)
{
   return arg;
}

// Verify that Outer class can be instantiated
void test ()
{
   Outer<int> ou;
}
\\\\\\\\\\  End nestempl.cc  //////////

To reiterate, Borland C++ 3.1 refuses to compile this, it complains about
syntax on the body of operator=.  Does the C++ language support what I'm trying
to do?  Is there a compiler that can compile this code?  Thanks for your time!

David Arnstein
arnstein@netcom.com




Author: jamshid@emx.cc.utexas.edu (Jamshid Afshar)
Date: 21 Sep 1993 22:30:07 -0500
Raw View
In article <arnsteinCDKBBw.J2A@netcom.com>,
David Arnstein <arnstein@netcom.com> wrote:

 // code very similar to David's
 template<class T> class Outer {
 public:
    class Inner {
    public:
       Inner& operator=(const Inner&); };
    };
    T whatever;
 };

 template<class T>
 Outer<T>::Inner&             /// most compilers break here ///
 Outer<T>::Inner::operator= (const Inner&)
 { return *this; }

 int main() {
    Outer<int>::Inner i,j;
    i = j;
 }

>To reiterate, Borland C++ 3.1 refuses to compile this, it complains about
>syntax on the body of operator=.Does the C++ language support what I'm trying
>to do?  Is there a compiler that can compile this code?  Thanks for your time!

I see nothing in the ARM prohibiting your code and I see no reason
that it should be made illegal.  As you wrote, the non-template version
is legal.

BC++, cfront 3.0.2, and gcc 2.4.5 all break on Dave's code.  BC++ and
gcc gives parse errors.  cfront does "not implement" out-of-line
definitions of functions belonging to a nested class template.  I
would bet Symantec C++ crashes.  I would also bet that Watcom C++ can
handle this.  If it doesn't I'll be very depressed about the state of
C++ compilers.

So, file a bug report if your compiler doesn't handle out-of-line
member function template definitions which return a nested type.
Another thing to make sure your compiler handles is:

 struct Vec { typedef int State; };

 template <class Container>
 struct Iter {
   inline Iter(const Container::State& s);
 };

 template <class Container>
 inline Iter<Container>::Iter(const Container::State& s) {}

 typedef Iter<Vec> IntIter;

All C++ compilers I tried break on it for one reason or another.  A
ray of hope is that Taumetric seems to have their act together wrt the
implementation of templates.  See the article in the latest C++ Report
written by the company's president(?).

Jamshid Afshar
jamshid@emx.cc.utexas.edu