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

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(unsigned 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<unsigned int INT>
class D: public B{
public:
static B& instance(){static D singleton; return singleton;}
};

class Get{
public:
Get()
:r0(D<0>::instance())
,r1(D<1>::instance())
,r2(D<2>::instance())
,r3(D<3>::instance())
,r4(D<4>::instance())
,r5(D<5>::instance()){
/* whatever else needed */
};
static B& instance_of_B(unsigned 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 1137
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(unsigned 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<unsigned int INT>
class D: public B{
public:
static B& instance(){static D singleton; return singleton;}
};

class Get{
public:
Get()
:r0(D<0>::instance())
,r1(D<1>::instance())
,r2(D<2>::instance())
,r3(D<3>::instance())
,r4(D<4>::instance())
,r5(D<5>::instance()){
/* whatever else needed */
};
static B& instance_of_B(unsigned 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
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
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
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,...
2
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...
150
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...
0
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...
19
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...
4
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...
2
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>...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...

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.