template & void argument

  • Thread starter Manuel Maria Diaz Gomez
  • Start date
M

Manuel Maria Diaz Gomez

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?

Cheers

Manuel





--
========================================================================
Manuel Diaz-Gomez | ATLAS Bldg. 32/SB-008 tel. +41 22 76 76304
CERN EP Division
CH-1211 Geneva 23
SWITZERLAND
========================================================================
 
D

Dave Moore

Manuel Maria Diaz Gomez said:
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.

Just overload the method with one that has no arguments ...

For example, (re-writing your sans errors 8*)

template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type

virtual void do_something() =0; // no argument version

};


class empty {};

class foo : public A<empty> {
public:
virtual void do_something() {}
};

Now the above should work fine, and be called through the no-argument
virtual function in the interface.

HTH, Dave Moore
 
J

Julie

Dave said:
Just overload the method with one that has no arguments ...

For example, (re-writing your sans errors 8*)

template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type

virtual void do_something() =0; // no argument version

};


class empty {};

class foo : public A<empty> {
public:
virtual void do_something() {}
};

Now the above should work fine, and be called through the no-argument
virtual function in the interface.

HTH, Dave Moore

Suppose that the problem requires that there is only 1 do_something method that
exactly and only corresponds to the type T.

In your example, for type int, the template expands to:

virtual void do_something(int) = 0;
void do_something() = 0; // not wanted for non-void types
 
T

tom_usenet

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
 
D

Dave Moore

Julie said:
Suppose that the problem requires that there is only 1 do_something method that
exactly and only corresponds to the type T.

Well, that wasn't in the original deal, 8*) but I think it is
reasonable to assume that you will know which version (with or without
argument) you want to override, so you can simply declare the other
one private in the derived class.

class foo2 : public A<int> {
public:
virtual void do_something(int); // override version with argument
private:
void do_something(); // hide version without argument
};

It is not an air-tight solution, but without seeing a more in-depth
example, I can't think of anything else. The TMP wizards might be
able to come up with a more elegant solution I suppose.

Dave Moore
 
D

Dave Moore

Well, that wasn't in the original deal, 8*) but I think it is
reasonable to assume that you will know which version (with or without
argument) you want to override, so you can simply declare the other
one private in the derived class.

class foo2 : public A<int> {
public:
virtual void do_something(int); // override version with argument
private:
void do_something(); // hide version without argument
};

It is not an air-tight solution, but without seeing a more in-depth
example, I can't think of anything else. The TMP wizards might be
able to come up with a more elegant solution I suppose.

Hmmm .. actually, it may not be a solution at all, because the no-arg
version is still public in the base class, so it could be called
through a pointer to the base class at run-time. I'm not sure what
would happen in this case if the object pointed-to was a derived type
with a private version of the function, but I guess it wouldn't be
good. Actually, I don't have much experience with run-time
polymorphism, so maybe I should keep my mouth (er, keyboard?) shut and
let the experts answer these questions. 8*)

OTOH, I *do* learn a lot when I get shot down by those more
knowledgable than me ... I just don't want to screw up the person I
was trying to help in the first place.

Dave Moore
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top