473,472 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

template class question (more elegant soln)

I want to be able to restrict the set of classes for which a template
class can be instantiated (i.e enforce that all instantiation MUST be
for classes taht derive from a base type BaseType).

This occured to me immediately, but use of a dummy variable is not
elegant - are there other (more elegant) ways of doing this?
template <class DerivedType>
class MyClass
{
public:
MyClass():m_type(*DerivedType){};
~MyClass(){}

private:
BaseType * m_type ; //dummy variable
};
Aug 31 '07 #1
3 1557
Anonymous wrote:
I want to be able to restrict the set of classes for which a template
class can be instantiated (i.e enforce that all instantiation MUST be
for classes taht derive from a base type BaseType).

This occured to me immediately, but use of a dummy variable is not
elegant - are there other (more elegant) ways of doing this?
template <class DerivedType>
class MyClass
{
public:
MyClass():m_type(*DerivedType){};
~MyClass(){}

private:
BaseType * m_type ; //dummy variable
};
The combination of 'Boost::enable_if' with 'base_or_derived' temlplate
helpers should essentially do what you ask, I think.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 31 '07 #2
Anonymous wrote:
I want to be able to restrict the set of classes for which a template
class can be instantiated (i.e enforce that all instantiation MUST be
for classes taht derive from a base type BaseType).

This occured to me immediately, but use of a dummy variable is not
elegant - are there other (more elegant) ways of doing this?
template <class DerivedType>
class MyClass
{
public:
MyClass():m_type(*DerivedType){};
~MyClass(){}

private:
BaseType * m_type ; //dummy variable
};

Something like this would be better:
template <class DerivedType>
class MyClass
{
static_assert(is_derived(DerivedType,BaseType));
public:
MyClass()
{}

private:
BaseType * m_type ; //dummy variable
};

I have not used them but I think you'll find static_assert and
is_derived in boost.
Aug 31 '07 #3
I am asking myself why do you want to do that?
I think in that case a template is not needed at all. You can make a
class that's holding a pointer (or more of them) to your BaseType as
an aggregation and you can assign any derivedType to that (these)
pointer(s).
But maybe you can explain why you need a template here.

Greetings

Aug 31 '07 #4

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

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
1
by: mast2as | last post by:
Hi there, Here is the conceptual problem i try to find an elegant solution to. I have a template class that I use to save data from a file. The data can be integer, float, double. This is a...
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
10
by: mast2as | last post by:
Is it possible to limit a template class to certain types only. I found a few things on the net but nothing seems to apply at compile time. template <typename T> class AClass { public:...
2
by: Gernot Frisch | last post by:
I have a struct, that has a lot of double variables. (only doubles, assume). Now, each variable has a different "default" value, which is set in Init function. So, is there any better way of...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
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
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,...
1
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...
1
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
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.