473,503 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

explicit instantiation of template methods of template classes

Hi everybody,

I am a little bit confused with the syntax of explicit instantiation,
and I am not sure that it is possible to do what I want to do.
Maybe someone has an idea.

Consider a template class:

template <class ABC>
struct CT : public ABC
{
template<typename T>
void a(T const &)
{
// something
}

// the next line is not enough for what I want, but probably needed
using ABC::a;
};
//... and an abstract base class:

struct ABC
{
virtual void a(int const &) = 0;
};

//... and a usage:

struct MyClass : public CT<ABC>
{

};
now CT<>::a hides ABC::a. But I would like to instantiate CT::a
so that it does not hide ABC::a, but implement the virtual
function ABC::a for MyClass.

Is this possible? what do I have to write into MyClass?
Any idea?
Tom
Jul 23 '05 #1
6 1998
Thomas Maier-Komor wrote:
Hi everybody,

I am a little bit confused with the syntax of explicit instantiation,
and I am not sure that it is possible to do what I want to do.
Maybe someone has an idea.

Consider a template class:

template <class ABC>
struct CT : public ABC
{
template<typename T>
void a(T const &)
{
// something
}

// the next line is not enough for what I want, but probably needed
using ABC::a;
};
Why is this needed?

//... and an abstract base class:

struct ABC
{
virtual void a(int const &) = 0;
};

//... and a usage:

struct MyClass : public CT<ABC>
{

};
now CT<>::a hides ABC::a. But I would like to instantiate CT::a
so that it does not hide ABC::a, but implement the virtual
function ABC::a for MyClass.

Is this possible? what do I have to write into MyClass?
Any idea?
Tom


I am not sure whether this is legal, but on my machine it appears to do the
intended:

#include <iostream>

struct A {

virtual void a ( void ) = 0;

virtual ~A ( void ) {}

};

template < typename T >
struct B : public T {

void a ( void ) {
std::cout << "hello world!\n";
}

~B ( void ) {}

};

struct C : public B<A> {

void a ( void ) {
std::cout << "don't say hello!\n";
}

~C ( void ) {}

};

struct D : public B<A> {

~D ( void ) {}

};
int main ( void ) {
A * a_ptr = new C;
a_ptr->a();
A * a2_ptr = new D;
a2_ptr->a();
}

Best

Kai-Uwe Bux
Jul 23 '05 #2
Kai-Uwe Bux wrote:
Thomas Maier-Komor wrote:

Hi everybody,

I am a little bit confused with the syntax of explicit instantiation,
and I am not sure that it is possible to do what I want to do.
Maybe someone has an idea.

Consider a template class:

template <class ABC>
struct CT : public ABC
{
template<typename T>
void a(T const &)
{
// something
}

// the next line is not enough for what I want, but probably needed
using ABC::a;
};

Why is this needed?

//... and an abstract base class:

struct ABC
{
virtual void a(int const &) = 0;
};

//... and a usage:

struct MyClass : public CT<ABC>
{

};
now CT<>::a hides ABC::a. But I would like to instantiate CT::a
so that it does not hide ABC::a, but implement the virtual
function ABC::a for MyClass.

Is this possible? what do I have to write into MyClass?
Any idea?
Tom

I am not sure whether this is legal, but on my machine it appears to do the
intended:

#include <iostream>

struct A {

virtual void a ( void ) = 0;

virtual ~A ( void ) {}

};

template < typename T >
struct B : public T {

void a ( void ) {
std::cout << "hello world!\n";
}

~B ( void ) {}

};

struct C : public B<A> {

void a ( void ) {
std::cout << "don't say hello!\n";
}

~C ( void ) {}

};

struct D : public B<A> {

~D ( void ) {}

};
int main ( void ) {
A * a_ptr = new C;
a_ptr->a();
A * a2_ptr = new D;
a2_ptr->a();
}

Best

Kai-Uwe Bux

OK that's obvious, but this does not employ my
template method, which should implement the pure
virtual method automagically. And _that_ is my
primary intention...

Tom
Jul 23 '05 #3
Thomas Maier-Komor wrote:
Kai-Uwe Bux wrote:
Thomas Maier-Komor wrote:

Hi everybody,

I am a little bit confused with the syntax of explicit instantiation,
and I am not sure that it is possible to do what I want to do.
Maybe someone has an idea.

Consider a template class:

template <class ABC>
struct CT : public ABC
{
template<typename T>
void a(T const &)
{
// something
}

// the next line is not enough for what I want, but probably needed
using ABC::a;
};

Why is this needed?

//... and an abstract base class:

struct ABC
{
virtual void a(int const &) = 0;
};

//... and a usage:

struct MyClass : public CT<ABC>
{

};
now CT<>::a hides ABC::a. But I would like to instantiate CT::a
so that it does not hide ABC::a, but implement the virtual
function ABC::a for MyClass.

Is this possible? what do I have to write into MyClass?
Any idea?
Tom

I am not sure whether this is legal, but on my machine it appears to do
the intended:

#include <iostream>

struct A {

virtual void a ( void ) = 0;

virtual ~A ( void ) {}

};

template < typename T >
struct B : public T {

void a ( void ) {
std::cout << "hello world!\n";
}

~B ( void ) {}

};

struct C : public B<A> {

void a ( void ) {
std::cout << "don't say hello!\n";
}

~C ( void ) {}

};

struct D : public B<A> {

~D ( void ) {}

};
int main ( void ) {
A * a_ptr = new C;
a_ptr->a();
A * a2_ptr = new D;
a2_ptr->a();
}

Best

Kai-Uwe Bux

OK that's obvious, but this does not employ my
template method, which should implement the pure
virtual method automagically. And _that_ is my
primary intention...


Clearly, I just do not understand what you are trying to accomplish. How is
the pure virtual method A::a not automatigically implemented in the class D
from my example? A::a clearly is implemented in D, and it is implemented
via inheriting from the template B<A>.
Sorry for not being helpful

Kai-Uwe Bux
Jul 23 '05 #4
Kai-Uwe Bux wrote:


Clearly, I just do not understand what you are trying to accomplish. How is
the pure virtual method A::a not automatigically implemented in the class D
from my example? A::a clearly is implemented in D, and it is implemented
via inheriting from the template B<A>.
Sorry for not being helpful

Kai-Uwe Bux


What I am trying to accomplish is the following:
In an implementation of the visitor pattern with several visitors,
I want an entry for each kind of Visitor to be implemented
automatically, so that a call to object->runVisitor(visitor)
is single dispatching (via object's base class) instead of double
dispatching (via object's and visitor's base classes). This
can be done coding manually, but I wanted the added functionallity
that I have a template class which can be configured to create
the necessary methods for different visitors.

I hope you understand what I mean...

Tom
Jul 23 '05 #5
Thomas Maier-Komor wrote:
Kai-Uwe Bux wrote:


Clearly, I just do not understand what you are trying to accomplish. How
is the pure virtual method A::a not automatigically implemented in the
class D from my example? A::a clearly is implemented in D, and it is
implemented via inheriting from the template B<A>.
Sorry for not being helpful

Kai-Uwe Bux


What I am trying to accomplish is the following:
In an implementation of the visitor pattern with several visitors,
I want an entry for each kind of Visitor to be implemented
automatically, so that a call to object->runVisitor(visitor)
is single dispatching (via object's base class) instead of double
dispatching (via object's and visitor's base classes). This
can be done coding manually, but I wanted the added functionallity
that I have a template class which can be configured to create
the necessary methods for different visitors.

I hope you understand what I mean...

Tom


Thanks for the explanation. Now I see more clearly what you want.
Unfortunately, I do not know how to do that. Did you look into:

Andrei Alexancrescu: Modern C++ Design

Chapter 10 is on the visitor pattern.
Best

Kai-Uwe Bux
Jul 23 '05 #6
Kai-Uwe Bux wrote:


Thanks for the explanation. Now I see more clearly what you want.
Unfortunately, I do not know how to do that. Did you look into:

Andrei Alexancrescu: Modern C++ Design

Chapter 10 is on the visitor pattern.
Best

Kai-Uwe Bux


I will have a look - thanks for the pointer.

Tom
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2592
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
4
1822
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
8
4779
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
8
11412
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
3
2427
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
3
3993
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
1
3056
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
4
2473
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
2880
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.