Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > explicit instantiation of template methods of template classes

Reply
Thread Tools

explicit instantiation of template methods of template classes

 
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      05-17-2005
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-17-2005
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
 
Reply With Quote
 
 
 
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      05-17-2005
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-17-2005
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
 
Reply With Quote
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      05-17-2005
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-18-2005
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
 
Reply With Quote
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      05-19-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of non-template member of template class Noah Roberts C++ 6 02-02-2011 09:53 PM
Explicit instantiation of incomplete template classes JurgenvonOerthel@hotmail.com C++ 2 01-22-2008 09:55 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM
problem with explicit template instantiation in Visual C++ 6.0 . C. Carbonera C++ 4 02-05-2004 05:05 PM



Advertisments