473,385 Members | 1,655 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,385 software developers and data experts.

Templates - Requiring typename to inherit from a particular class?

Hi,

Is it possible to force the typename of a template to inherit from a
particular class? For example, something like this (conceptual; this
doesn't compile):

class Parent {
public:
[...]
virtual void vfn() = 0;
};

template <child-of-Parent T>
class CC {
[...]
void foo() {
vfn();
}
};

In other words, can I force T to be a child of Parent?

Thanks
Oct 21 '05 #1
6 2240
On Thu, 20 Oct 2005 20:15:22 -0800, bl**@blah.blah wrote:
Hi,

Is it possible to force the typename of a template to inherit from a
particular class? For example, something like this (conceptual; this
doesn't compile):

class Parent {
public:
[...]
virtual void vfn() = 0;
};

template <child-of-Parent T>
class CC {
[...]
void foo() {
vfn();
}
};

In other words, can I force T to be a child of Parent?

Thanks

My mistake, the line in foo() should be:
T bar;
bar.vfn();

Sorry.
Oct 21 '05 #2
bl**@blah.blah wrote:
Is it possible to force the typename of a template to inherit from a
particular class? For example, something like this (conceptual; this
doesn't compile):

class Parent {
public:
[...]
virtual void vfn() = 0;
};

template <child-of-Parent T>
class CC {
[...]
void foo() {
vfn();
}
};

In other words, can I force T to be a child of Parent?


What you can do is issue a compile-time assertion using Boost's
'is_base_and_derived'. See the news archives for suggestions.

V
Oct 21 '05 #3

<bl**@blah.blah> wrote in message
news:r3********************************@4ax.com...
| Hi,
|
| Is it possible to force the typename of a template to inherit from a
| particular class? For example, something like this (conceptual; this
| doesn't compile):
|
| class Parent {
| public:
| [...]
| virtual void vfn() = 0;
| };
|
| template <child-of-Parent T>

you meant:

template< class Child_of_Parent >

or

template< typename Child_of_Parent >

| class CC {
| [...]
| void foo() {
| vfn();
| }
| };
|
| In other words, can I force T to be a child of Parent?
|

You already have set that requirement. The call to vfn() above is only
valid if its implemented somewhere. The Parent class is abstract *and*
its vfn() is pure virtual. But be careful, if you don't provide an
access specifier (ie: Child_of_Parent::vfn(); ) you may be in for a
surprise (perhaps even a good surprise). Think GrandChild.
Oct 21 '05 #4
Peter_Julian wrote:
<bl**@blah.blah> wrote in message
news:r3********************************@4ax.com...
| Hi,
|
| Is it possible to force the typename of a template to inherit from a
| particular class? For example, something like this (conceptual; this
| doesn't compile):
|
| class Parent {
| public:
| [...]
| virtual void vfn() = 0;
| };
|
| template <child-of-Parent T>

you meant:

template< class Child_of_Parent >

or

template< typename Child_of_Parent >

| class CC {
| [...]
| void foo() {
| vfn();
| }
| };
|
| In other words, can I force T to be a child of Parent?
|

You already have set that requirement. The call to vfn() above is only
valid if its implemented somewhere. The Parent class is abstract *and*
its vfn() is pure virtual. But be careful, if you don't provide an
access specifier (ie: Child_of_Parent::vfn(); ) you may be in for a
surprise (perhaps even a good surprise). Think GrandChild.

That doesn't /quite/ work, because templates are perfectly happy to call
vfn's which aren't member functions in classes derived from Parent.

For instance:
class NotChild {
public:
void vfn() { /* !!! */ }
};

CC<NotChild> is perfectly valid and works just fine, even though it
doesn't derive from Parent.

Saying Child_of_Parent::vfn only works happily if you only have one
child of Parent. Otherwise, you have to template it and end up saying:
T bar;
bar.T::vfn();

Which doesn't really do anything.
Oct 21 '05 #5

bl**@blah.blah wrote:
Hi,

Is it possible to force the typename of a template to inherit from a
particular class? For example, something like this (conceptual; this
doesn't compile):

class Parent {
public:
[...]
virtual void vfn() = 0;
};

template <child-of-Parent T>
class CC {
[...]
void foo() {
vfn();
}
};

In other words, can I force T to be a child of Parent?


Why do you want to restrict what other people can do with your
template? That's their business.

The fact is that your template can be used in any situation which will
satisfy the generated call vfn() in the foo() function.

But I suspect you may be looking for a "mixin" template:

template <class T>
class CC : public T {
// ...
void foo { vfn(); }
}

Now T has to be a class, because it's used by inheritance. If the class
T has a function U vfn() where U is some type, possibly void, that
function will be inherited and used to satisfy the vfn() call.

T can be anything which has a vfn() function. If you have a non-member
vfn() function in scope, that can be used too! To prevent the use of a
file scope function, and insist on it coming from T, I think you can do
this:

void foo { T::vfn(); }

Beyond that, there is no need to restrict T to be a particular class.

Oct 21 '05 #6
On 21 Oct 2005 11:11:10 -0700, "Kaz Kylheku" <kk******@gmail.com>
wrote:

bl**@blah.blah wrote:
Hi,

Is it possible to force the typename of a template to inherit from a
particular class? For example, something like this (conceptual; this
doesn't compile):

class Parent {
public:
[...]
virtual void vfn() = 0;
};

template <child-of-Parent T>
class CC {
[...]
void foo() {
vfn();
}
};

In other words, can I force T to be a child of Parent?
Why do you want to restrict what other people can do with your
template? That's their business.

The fact is that your template can be used in any situation which will
satisfy the generated call vfn() in the foo() function.


You're absolutely right. I guess I didn't fully think the problem
through beforehand. I don't care what anyone else tries to do with
the template, I just want to be able to call vfn(). I just didn't
realize the compiler would let me do this. This will work fine.

Thanks everyone for your responses.

But I suspect you may be looking for a "mixin" template:

template <class T>
class CC : public T {
// ...
void foo { vfn(); }
}

Now T has to be a class, because it's used by inheritance. If the class
T has a function U vfn() where U is some type, possibly void, that
function will be inherited and used to satisfy the vfn() call.

T can be anything which has a vfn() function. If you have a non-member
vfn() function in scope, that can be used too! To prevent the use of a
file scope function, and insist on it coming from T, I think you can do
this:

void foo { T::vfn(); }

Beyond that, there is no need to restrict T to be a particular class.

Oct 21 '05 #7

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

Similar topics

1
by: skscpp | last post by:
I have a question regarding "optional" inheritance in regards to template. This is not compiled code. template<typename T, typename P1 = Invalid<1>, typename P2 = Invalid<2>, typename P3 =...
3
by: Drew McCormack | last post by:
Does operator() get inherited? I would have thought so, but I can't get the following to work: I have a base class that takes its derived class as a template template parameter (the ...
2
by: Steven T. Hatton | last post by:
While thunbing through _C++ Templates, The Complete Guide_ (reckon I aught to read it?) I came across a discussion of using templates to "unroll" loops. I thought that looked like a good idea, so...
2
by: tirzanello | last post by:
Hi guys, I'm becoming crazy with templates, maybe for it's a trivial problem... but I can't go through :-( any hint will be appreciated. I show you without templates a piece of code: 2 classes,...
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
5
by: Zeppe | last post by:
Hi all! my problem is this one, I think that it could be a common one, maybe a pattern, so if you can help me somehow it would be great. Let's suppose I have a class Base class Base { //...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
4
by: Bit Byter | last post by:
I want to write a (singleton) container for instances of my class templates, however, I am not too sure on how to: 1). Store the instances 2). How to write the acccesor method (instance()) to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.