Topic: Template methods in classes


Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/08/02
Raw View
hans_j_micheelsen@my-deja.com wrote:

> It seems that the compiler accepts the class declaration. So maybe it's
> the way I call the template method that is wrong. I have just made a
> (much) smaller example:
>
> class CPing
> {
>   template <typename T> void Func() {};
> };
>
> int main()
> {
>   CPing querty;
>   querty.Func < int >  ();
> }

This looks right, according to how I read the standard. The compiler I tried
accepts it.

I think it's a bug or missing feature in Visual C++ 6.0 with service pack 3.

    -- Darin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: hans_j_micheelsen@my-deja.com
Date: 1999/08/02
Raw View
Please forgive me. I forgot to tell. My compiler is Visual C++ 6.0 with
service pack 3.

It seems that the compiler accepts the class declaration. So maybe it's
the way I call the template method that is wrong. I have just made a
(much) smaller example:

class CPing
{
  template <typename T> void Func() {};
};

int main()
{
  CPing querty;
  querty.Func < int >  ();

  return 0;
}


In article <7nsvu0$fjp$1@engnews1.eng.sun.com>,
  clamage@eng.sun.com (Steve Clamage) wrote:
> hans_j_micheelsen@my-deja.com writes:
>
> >Is it possible to have template methods in classes? The class need
not
> >to be a template class itself. The problem is that I want to give the
> >type as a parameter rather than the actual parameter.
>
> The C++ standard allows templates as members of classes, as well
> as members of class templates. For example,
> class C1 {
>     template<class T> ... ; // class or function template
> };
> template <class T> class C1 {
>     template<class U> ... ; // class or function template
> };
>
> I think the only restriction on member templates is that a
> member function template cannot be virtual. (It would otherwise
> greatly complicate the implementation of virtual functions --
> the total number and identity of template instantiations for
> the virtual function template would not be known in any one
> translation unit.)
>
> Not all compilers currently support templates as class or template
> members.

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: hans_j_micheelsen@my-deja.com
Date: 1999/07/30
Raw View
Is it possible to have template methods in classes? The class need not
to be a template class itself. The problem is that I want to give the
type as a parameter rather than the actual parameter.

Below I have listed an example of what I want to do. I have a testsuite
engine that runs some testcases in sequential order. The testcases are
enrolled to the testsuite as pointers to objects of classes derived from
CTestCase. the enrolment is done in the ctor of CTestSuite. Normally I
have several very specific testcases derived from CTestCase. In this
case only one is shown.

Can anyone help me?

#include <vector>
using namespace std;

class CTestCase
{
};

class CMyTestCase : public CTestCase
{
};

class CTestSuite
{
public:
  CTestSuite(void) {};
  virtual ~CTestSuite(void) { /* delete content of m_rg ...*/};
  template <typename T> void EnrolTestCase(void); // Template method
private:
  vector<CTestCase *> m_rg;
};

template <typename T>
void CTestSuite::EnrolTestCase(void)
{
  T *var = new T;
  m_rg.push_back(var);
}

class CMyTestSuite : public CTestSuite
{
public:
  CMyTestSuite(void);
  virtual ~CMyTestSuite(void) {};
};

CMyTestSuite::CMyTestSuite(void)
{
  // I have tried this
  EnrolTestCase<CMyTestCase>(); // Error: 'CMyTestCase' : illegal use of
this type as an expression
  // and even this, just to see what that gave...
  EnrolTestCase(); //Error: could not deduce template argument for 'T'
};


void main(void)
{
  CMyTestSuite tc;

//  etc. etc. etc.
}


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/07/31
Raw View
hans_j_micheelsen@my-deja.com wrote:

> Is it possible to have template methods in classes? The class need not
> to be a template class itself. The problem is that I want to give the
> type as a parameter rather than the actual parameter.

This is possible. It's discussed in section 14.5.2 [temp.mem] of the
standard.

Of course, there's no guarantee that your compiler supports it.

For example, my compiler supports template members in classes, but the
definition must be in the class definition if the class is itself not a
template. Don't ask. They've promised to improve it in the next release.

>   // I have tried this
>   EnrolTestCase<CMyTestCase>(); // Error: 'CMyTestCase' : illegal use of
> this type as an expression

I believe this demonstrates that the feature isn't implemented properly in
your compiler. The function invocation looks correct to me and I was able to
compile your example, including this line, with my compiler by moving the
definition of CTestSuite::EnrolTestCase() into the class definition.

> void main(void)

This should be "int main()". The first void is incorrect, the second one
unnecessary. The use of "(void)" throughout your example is an unnecessary
ISO-C-ism. In C++, "()" works just fine for no parameters, although "(void)"
is allowed for compatibility with C.

    -- Darin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/07/31
Raw View
hans_j_micheelsen@my-deja.com writes:

>Is it possible to have template methods in classes? The class need not
>to be a template class itself. The problem is that I want to give the
>type as a parameter rather than the actual parameter.

The C++ standard allows templates as members of classes, as well
as members of class templates. For example,
class C1 {
    template<class T> ... ; // class or function template
};
template <class T> class C1 {
    template<class U> ... ; // class or function template
};

I think the only restriction on member templates is that a
member function template cannot be virtual. (It would otherwise
greatly complicate the implementation of virtual functions --
the total number and identity of template instantiations for
the virtual function template would not be known in any one
translation unit.)

Not all compilers currently support templates as class or template
members.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]