473,769 Members | 5,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

breaking template parameter dependence

er
Hi All,

I have a class D<INTwhich serves a small implementation purpose
(think for example D<INTis Factorial<INT>) . I don't want every class
that uses D<INTto depend on template parameter INT. However I know
that I won't need more than say INT=5. So I do 2 things:
a) have D derive from a base class B (see below)
b) have a class Get with a member B& instance_of_B(u nsigned int INT)
(see below). However I can't put references to B into an vector
because D<INTis a singleton (non-copyable/assignable). do I have to
go through the trouble of creating a (copyable) wrapper around each
D<INT>& or is there an easier way (based on the code below)?
class B;//abstract class defining an interface

template<unsign ed int INT>
class D: public B{
public:
static B& instance(){stat ic D singleton; return singleton;}
};

class Get{
public:
Get()
:r0(D<0>::insta nce())
,r1(D<1>::insta nce())
,r2(D<2>::insta nce())
,r3(D<3>::insta nce())
,r4(D<4>::insta nce())
,r5(D<5>::insta nce()){
/* whatever else needed */
};
static B& instance_of_B(u nsigned int i){
// intended behaviour:
// instance_of_B(0 ) returns r0
// instance_of_B(1 ) returns r1
// instance_of_B(2 ) returns r2
// instance_of_B(3 ) returns r3
// instance_of_B(4 ) returns r4
// instance_of_B(5 ) returns r5

};
private:
B& r0;
B& r1;
B& r2;
B& r3;
B& r4;
B& r5;
};
Dec 1 '07 #1
1 1159
er wrote:
I have a class D<INTwhich serves a small implementation purpose
(think for example D<INTis Factorial<INT>) . I don't want every class
that uses D<INTto depend on template parameter INT. However I know
that I won't need more than say INT=5. So I do 2 things:
a) have D derive from a base class B (see below)
b) have a class Get with a member B& instance_of_B(u nsigned int INT)
(see below). However I can't put references to B into an vector
You can't put references to anything in a vector. You can, however,
put _pointers_ to your objects in a vector, because while your D<>
are singletons, pointers to it can be multiplied at will. So can
pointers to B, which still provide polymorphism, don't they?

In the future, will you please specify how your class is going to be
used? You give some abstract representation of some idea you have,
and it's supposed to serve some concrete purpose (otherwise why do
you create it?) and then we need to imagine what purpose you have
in mind. We are not mind readers, you know.

The suitability of any particular design is verified against the
problem it solves, not against another similar solution.
because D<INTis a singleton (non-copyable/assignable). do I have to
go through the trouble of creating a (copyable) wrapper around each
D<INT>& or is there an easier way (based on the code below)?
What's wrong with

vector<B*>

(considering your definition of 'B', of course)?
>

class B;//abstract class defining an interface

template<unsign ed int INT>
class D: public B{
public:
static B& instance(){stat ic D singleton; return singleton;}
};

class Get{
public:
Get()
:r0(D<0>::insta nce())
,r1(D<1>::insta nce())
,r2(D<2>::insta nce())
,r3(D<3>::insta nce())
,r4(D<4>::insta nce())
,r5(D<5>::insta nce()){
/* whatever else needed */
};
static B& instance_of_B(u nsigned int i){
// intended behaviour:
// instance_of_B(0 ) returns r0
// instance_of_B(1 ) returns r1
// instance_of_B(2 ) returns r2
// instance_of_B(3 ) returns r3
// instance_of_B(4 ) returns r4
// instance_of_B(5 ) returns r5

};
private:
B& r0;
B& r1;
B& r2;
B& r3;
B& r4;
B& r5;
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 1 '07 #2

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

Similar topics

14
2588
by: John Harrison | last post by:
The following code does not compile template <class Derived> struct X { typedef typename Derived::type type; }; struct Y : public X<Y> {
7
2131
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
8
3153
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int, 1> and A<int, 2> are different types. Now, if the A template
2
2763
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type parameter, e.g. template<class T> - parameters with *simple* types like int, e.g. template<int size> - template parameter, e.g. template< template<class> class A > the latter beeing a type specifier, not an instance of A.
150
6579
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and see if you agree with my opinion or not. Tony Marston http://www.tonymarston.net
0
1919
by: Siphiuel | last post by:
Hi everyone. When using visitor pattern, we have a nasty dependence on the types of visitable objects that is coded way on top on the visitor hierarchy. i mean, like this: class AbstractVisitor { public: virtual void visit(Object_type_1 *); virtual void visit(Object_type_2 *)
19
2565
by: n.torrey.pines | last post by:
I have the following tree definition: template<typename T> struct tree { T first; vector<tree<T second; // branches }; which compiles successfully. What I'd like to do though is to use another template like 'pair', because I might have a bunch of useful
4
3015
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the template parameter is 2 or 3; for efficiency, I do not wish to perform the calculations if the template parameter is 1. I currently do this as follows:
2
2380
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector> class Ring {
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.