Topic: virtual templated function in templated class


Author: emptystate@yahoo.co.uk (Empty State)
Date: Mon, 2 Jun 2003 18:38:16 +0000 (UTC)
Raw View
Hi,

I want to make a virtual templated function in a templated class.
I don't understand why g++ (3.2) won't let me compile, the error
message is

test-temp.h:10: declaration does not declare anything
test-temp.h:10: ISO C++ forbids declaration of `type name' with no
type
test-temp.h:10: virtual outside class declaration
test-temp.h:10: parse error before `template'
...

Some example code is given below, make believe that icecream has
many types of flavour, the cone can be single, double, wide, etc
and when you make a cone you apply a topping. If I remove the
"virtual" from the function cone() then everything compiles OK.

TIA

koan

/** test-temp.h: **/

#include <iostream>
using namespace std;

template <class flavour> class icecream
{
  public:
  icecream() {}

  virtual template <class topping> void cone(int a, int b, topping
abcm);

  flavour counter;
};

template <class flavour> template <class topping> void
icecream<flavour>::cone(int a, int b, topping abcdm)
{
  int temporary = (int)(abcdm);
  std::cout << "Virtual templated function " << a + b + temporary <<
"\n";
}


/** main.cc: **/

#include "test-temp.h"

int main()
{
  icecream<int> koan;

  koan.counter = 0;
  koan.cone(1, 2, 3.3);
}

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Mon, 2 Jun 2003 20:05:17 +0000 (UTC)
Raw View
"Empty State" <emptystate@yahoo.co.uk> wrote in message news:3852b54e.0306020616.565329c6@posting.google.com...
> Hi,
>
> I want to make a virtual templated function in a templated class.
> I don't understand why g++ (3.2) won't let me compile, the error
> message is
>
You can't have templated virtual member functions.   Doesn't matter
if the containing class is a template or not.    Too much clairvoyance
required as to when a template would need to be instantiated in the derived
class.

You can have templated non-virtual functions and you can have non-templated
virtual functions (even if they are inside a templated class).


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: heliosc@mindspring.com (Rayiner Hashem)
Date: Mon, 2 Jun 2003 21:49:05 +0000 (UTC)
Raw View
Virtual methods cannot be templated. See this thread for why:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=7f2735a5.0107101516.38fabed4%40posting.google.com&rnum=1&prev=/groups%3Fq%3DC%252B%252B%2Bvirtual%2Bmember%2Btemplates%26hl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3D7f2735a5.0107101516.38fabed4%2540posting.google.com%26rnum%3D1

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Tue, 3 Jun 2003 17:58:53 +0000 (UTC)
Raw View
On Mon, 2 Jun 2003 18:38:16 +0000 (UTC), emptystate@yahoo.co.uk (Empty
State) wrote:

Two problems.  First the syntax is wrong.

>  virtual template <class topping>
>  void cone(int a, int b, topping abcm);

   template <class topping>
   virtual void cone (int a, int b, topping abcm);

Second, it is not allowed.  The correct syntax gives you a meaningful
error message.

John

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]