473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

templates and inheritance

er
i have

class Impl; //abstract
class Impl_A:public Impl{...};//abstract
class Impl_B:public Impl{...};//abstract
class Impl_A_0: public Impl_A{/* implements */};

class S{ public: S(Impl*,...); ...};
class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){};
class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
//hierarchy ends here

template<typename T,typename U>
std::vector<Umult(T* pimpl){
//calls U(pimpl,...);
};

a) std::vector<S_Avec(mult<Impl_A0*,S_A>(Impl_A0*)); //ok
b) std::vector<S_Avec(mult(Impl_A0*)); //compiler error: no
matching function

i would have thought that T and U are implicitly specified by the
input and return type, respectively, so that b) would work?

Overloading like this does not work because i loose information about
the leaf class (Impl_A0* in example):
std::vector<S_Amult_A(Impl_A* pimpl){return mult(pimpl);}
std::vector<S_Bmult_A(Impl_B* pimpl){return mult(pimpl);}

any suggestion?

Sep 18 '07 #1
5 1314
er wrote:
i have

class Impl; //abstract
class Impl_A:public Impl{...};//abstract
class Impl_B:public Impl{...};//abstract
class Impl_A_0: public Impl_A{/* implements */};

class S{ public: S(Impl*,...); ...};
class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){};
class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
//hierarchy ends here

template<typename T,typename U>
std::vector<Umult(T* pimpl){
//calls U(pimpl,...);
};

a) std::vector<S_Avec(mult<Impl_A0*,S_A>(Impl_A0*)); //ok
OK -- what? 'vec' is a declaration of a function here, methinks.
b) std::vector<S_Avec(mult(Impl_A0*)); //compiler error: no
matching function

i would have thought that T and U are implicitly specified by the
input and return type, respectively, so that b) would work?
'vec' has overloaded constructors. How should the compiler know
that you intend to use the copy constructor? Yes, if it knew that
the intended specialisation of 'mult' returns a 'vec<S_A>', then
it might figure out that the copy c-tor is needed. But to know
what 'mult' to specialise (and how), it needs to know what your
'vec' constructor accepts as the argument. The classic catch-22.
Overloading like this does not work because i loose information about
the leaf class (Impl_A0* in example):
std::vector<S_Amult_A(Impl_A* pimpl){return mult(pimpl);}
std::vector<S_Bmult_A(Impl_B* pimpl){return mult(pimpl);}

any suggestion?
What exactly are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #2
er
On Sep 18, 6:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
er wrote:
i have
class Impl; //abstract
class Impl_A:public Impl{...};//abstract
class Impl_B:public Impl{...};//abstract
class Impl_A_0: public Impl_A{/* implements */};
class S{ public: S(Impl*,...); ...};
class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){};
class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
//hierarchy ends here
template<typename T,typename U>
std::vector<Umult(T* pimpl){
//calls U(pimpl,...);
};
a) std::vector<S_Avec(mult<Impl_A0*,S_A>(Impl_A0*)); //ok

OK -- what? 'vec' is a declaration of a function here, methinks.
b) std::vector<S_Avec(mult(Impl_A0*)); //compiler error: no
matching function
i would have thought that T and U are implicitly specified by the
input and return type, respectively, so that b) would work?

'vec' has overloaded constructors. How should the compiler know
that you intend to use the copy constructor? Yes, if it knew that
the intended specialisation of 'mult' returns a 'vec<S_A>', then
it might figure out that the copy c-tor is needed. But to know
what 'mult' to specialise (and how), it needs to know what your
'vec' constructor accepts as the argument. The classic catch-22.
Overloading like this does not work because i loose information about
the leaf class (Impl_A0* in example):
std::vector<S_Amult_A(Impl_A* pimpl){return mult(pimpl);}
std::vector<S_Bmult_A(Impl_B* pimpl){return mult(pimpl);}
any suggestion?

What exactly are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
perhaps i should clarify that vec is the name i give to my vector that
i initialize with the output from mult.
Sep 18 '07 #3
er wrote:
On Sep 18, 6:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>er wrote:
>>i have
>>class Impl; //abstract
class Impl_A:public Impl{...};//abstract
class Impl_B:public Impl{...};//abstract
class Impl_A_0: public Impl_A{/* implements */};
>>class S{ public: S(Impl*,...); ...};
class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){};
class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
//hierarchy ends here
>>template<typename T,typename U>
std::vector<Umult(T* pimpl){
//calls U(pimpl,...);
};
>>a) std::vector<S_Avec(mult<Impl_A0*,S_A>(Impl_A0*)); //ok

OK -- what? 'vec' is a declaration of a function here, methinks.
>>b) std::vector<S_Avec(mult(Impl_A0*)); //compiler error: no
matching function
>>i would have thought that T and U are implicitly specified by the
input and return type, respectively, so that b) would work?

'vec' has overloaded constructors. How should the compiler know
that you intend to use the copy constructor? Yes, if it knew that
the intended specialisation of 'mult' returns a 'vec<S_A>', then
it might figure out that the copy c-tor is needed. But to know
what 'mult' to specialise (and how), it needs to know what your
'vec' constructor accepts as the argument. The classic catch-22.
>>Overloading like this does not work because i loose information
about the leaf class (Impl_A0* in example):
std::vector<S_Amult_A(Impl_A* pimpl){return mult(pimpl);}
std::vector<S_Bmult_A(Impl_B* pimpl){return mult(pimpl);}
>>any suggestion?

What exactly are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

perhaps i should clarify that vec is the name i give to my vector that
i initialize with the output from mult.
The statement

vector<intvec(mult(char*));

is not a declaration of a vector. And if 'mult' is a function, it is
a syntax error because a type is passed to the function instead of
a value.

Please follow the recommendations of FAQ 5.8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #4
er
On Sep 18, 7:07 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
er wrote:
On Sep 18, 6:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
er wrote:
i have
>class Impl; //abstract
class Impl_A:public Impl{...};//abstract
class Impl_B:public Impl{...};//abstract
class Impl_A_0: public Impl_A{/* implements */};
>class S{ public: S(Impl*,...); ...};
class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){};
class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
//hierarchy ends here
>template<typename T,typename U>
std::vector<Umult(T* pimpl){
//calls U(pimpl,...);
};
>a) std::vector<S_Avec(mult<Impl_A0*,S_A>(Impl_A0*)); //ok
OK -- what? 'vec' is a declaration of a function here, methinks.
>b) std::vector<S_Avec(mult(Impl_A0*)); //compiler error: no
matching function
>i would have thought that T and U are implicitly specified by the
input and return type, respectively, so that b) would work?
'vec' has overloaded constructors. How should the compiler know
that you intend to use the copy constructor? Yes, if it knew that
the intended specialisation of 'mult' returns a 'vec<S_A>', then
it might figure out that the copy c-tor is needed. But to know
what 'mult' to specialise (and how), it needs to know what your
'vec' constructor accepts as the argument. The classic catch-22.
>Overloading like this does not work because i loose information
about the leaf class (Impl_A0* in example):
std::vector<S_Amult_A(Impl_A* pimpl){return mult(pimpl);}
std::vector<S_Bmult_A(Impl_B* pimpl){return mult(pimpl);}
>any suggestion?
What exactly are you trying to accomplish?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
perhaps i should clarify that vec is the name i give to my vector that
i initialize with the output from mult.

The statement

vector<intvec(mult(char*));

is not a declaration of a vector. And if 'mult' is a function, it is
a syntax error because a type is passed to the function instead of
a value.

Please follow the recommendations of FAQ 5.8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
ok, i see what you mean. please consider:

ImplA0* pimpl=new Impl_A0;
a) std::vector<S_Avec=mult<Impl_A0,S_A>(pimpl); //ok
b) std::vector<S_Avec=mult(pimpl); //compiler error
Sep 18 '07 #5
er wrote:
On Sep 18, 7:07 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>er wrote:
>>On Sep 18, 6:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
er wrote:
i have
>>>>class Impl; //abstract
class Impl_A:public Impl{...};//abstract
class Impl_B:public Impl{...};//abstract
class Impl_A_0: public Impl_A{/* implements */};
>>>>class S{ public: S(Impl*,...); ...};
class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){};
class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
//hierarchy ends here
>>>>template<typename T,typename U>
std::vector<Umult(T* pimpl){
//calls U(pimpl,...);
};
[..]

ok, i see what you mean. please consider:

ImplA0* pimpl=new Impl_A0;
a) std::vector<S_Avec=mult<Impl_A0,S_A>(pimpl); //ok
b) std::vector<S_Avec=mult(pimpl); //compiler error
Change your 'mult' definition to

template<class U, class T>
std::vector<Umult(T* pimpl) { ...

Then you could do

std::vector<S_Avec = mult<S_A>(pimpl);

The reason it won't work without <S_Aafter 'mult' is that
you cannot expect the compiler to deduce the 'U' type without
the argument and unclear return value type (see my mention
of 'catch-22' earlier). The deduction of the template return
value type can only successfully happen if a function pointer
is initialised from a template:

vector<int(*pf)(char*) = mult;

(should deduce 'U' as 'int' and 'T' as 'char').

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 19 '07 #6

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

Similar topics

1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
4
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although...
6
by: Ravi Rao | last post by:
Hi, This is about "templates vs inheritance" (please, before you flame me, I do understand that they cover largely non-intersecting domains). Apart from D&E*, I've found either online...
3
by: Paulo Matos | last post by:
Hi all, I'm curious if I can say in C++ that a certain template can only be instantiated (template-wise) by a class implementing a certain interface (inheriting from a given abstract class). ...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
2
by: Isaac Gelado | last post by:
Hi, I am having problems with inheritance in templates classes. Say I have the following classes: class A {}; class B: public A {}; template<typename Tclass C {}; Now in my code I have...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.