Re: template & void argument

From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 04/29/04


Date: Thu, 29 Apr 2004 21:55:45 +0100

On Thu, 29 Apr 2004 12:07:17 +0200, "Manuel Maria Diaz Gomez"
<Manuel.Maria.Diaz.Gomez@cern.ch> wrote:

>Thanks Victor for your previous answer!
>
>I have the following problem:
>
>I would like to implement an interface class that defines a pure virtual
>method that could take any parameter as input, void inlcuded.
>The first idea was to use a template, but in that case void is not admited
>as a type right?
>
>i.e.
>template<typename T>
>class A {
>
>virtual do_something(T) = 0;
>
>};
>
>Then, one of the derive classes can't just do :
>
>do_something(){...}
>
>Because the compiler will complain for void not being a type...
>
>Any work-around this?

Specialize on void (which is a bit of a pain, I know):

template<>
class A<void>
{
  virtual do_something() = 0;
};

You can always add a base class that doesn't need void specialization
to avoid duplicating any work between A<T> and A<void>.

Tom

-- 
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


Relevant Pages