473,888 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Customizing structure with policy classes

Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );

to create an object of ClassA. I would like to change this to

ClassA<VectorPo licy<double ca(6);

2. The output of this code is as follows:

before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()

before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb

I don't like the idea of instantiating the class with the default
constructor if I'm using only a parameter constructor. Why is this?
If I remove the default constructor then I have a compiler error:
main.cpp: In constructor 'ClassA<T, StoragePolicy>: :ClassA(size_t)
[with T = double, StoragePolicy = BitSetPolicy]':
main.cpp:110: instantiated from here
main.cpp:87: error: no matching function for call to
'BitSetPolicy<d ouble>::BitSetP olicy()'
main.cpp:60: note: candidates are:
BitSetPolicy<T> ::BitSetPolicy( size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<do uble>::BitSetPo licy(const BitSetPolicy<do uble>&)

I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?

3. I also tried to include in the policies, a create(size_t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/

Maybe some of my questions are too basic, so I apoligize and I
appreciate any help you can give me.

Alejandro Aragón

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset. hpp>

using boost::dynamic_ bitset;

using namespace std;

template <class T>
class StoragePolicy
{
public:

StoragePolicy() ;
StoragePolicy(s ize_t size_);

void print();

};

template <class T>
struct VectorPolicy
{
public:

typedef std::vector<TSt ructure;

VectorPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

VectorPolicy(si ze_t size_){
cout<<"Paramete r constructor"<<e ndl;
str_ = new vector<T>(size_ );
}

void print(){
cout<<"VectorPo licy print()"<<endl;
}

private:

Structure* str_;
};

template <class T>
struct BitSetPolicy
{
public:

typedef boost::dynamic_ bitset<Structur e;

BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

BitSetPolicy(si ze_t size_){
cout<<"Paramete r constructor"<<e ndl;
str_ = new dynamic_bitset< >(size_);
}

void print(){
cout<<"BitSetPo licy print()"<<endl;
}

private:

Structure* str_;
};

template
<
class T,
template <classclass StoragePolicy
>
class ClassA : public StoragePolicy<T >
{

StoragePolicy<T >* structure_;

public:

// parameter constructor
ClassA(size_t size_) {
structure_ = new StoragePolicy<T >(size_);
}

// print function
void print()
{
cout<<"ClassA print()"<<endl;
structure_->print();
}
};
int main()

{
cout<<"before instantiation of ca"<<endl;
ClassA<double,V ectorPolicyca(6 );
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,B itSetPolicycb(6 );
cout<<"after instantiation of cb"<<endl;


return 0;
}

Nov 5 '06 #1
11 2357
aaragon wrote:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
Dear aaragon:

i have veiwed your code listing .i am affraid
it looks to me that you are no c++ programer .

i do not claim to be professional
and i dont know Alexanderscue either

i did not understand what the program is intended to do
and i doubt that you can get a program
that satisfies your expectations
but assuming that you know what you are doing
i have the following recommendations for you:

you wrote:
>template <class T>
class StoragePolicy
{
public:
StoragePolicy() ;
StoragePolicy(s ize_t size_);
void print();

};
this portion of the code looks meaningless and error prone
remove or comment it out

>1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );
to create an object of ClassA. I would like to change this to
ClassA<VectorPo licy<double ca(6);

you have defined the class as:

template
<
class T,
template <classclass StoragePolicy

class ClassA : public StoragePolicy<T >
{
this misses a '>' before 'class ClassA' and causes
the compiler not to compile the code, further more
this fraction of the code in its current state
(after adding the afformentioned missing '>')
compiles on microsoft platform but i am not sure that it
is standard and portable to other platforms such as
borland. therefore i agree with you in changing
the code to this:

template
<
class StoragePolicy
>
class ClassA : public StoragePolicy
{

then you have to type the code as:

ClassA<VectorPo licy<double ca(6);

but in order to have a more consistent code
optionally:
1. you can type
typedef T value_type;
after the first '{'
following 'struct VectorPolicy' and 'struct BitSetPolicy'

2.if and only if you did the first ,type

typedef typename StoragePolicy:: value_type value_type;

after the first '{'
following 'class ClassA'
>
2. The output of this code is as follows:
before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()
before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb
I don't like the idea of instantiating the class with the default
constructor
you have Writen:

class ClassA : public StoragePolicy<T >

remove or comment out ' : public StoragePolicy<T >'
and the output will not contain:
default StoragePolicy constructor

alternatively if you have changed the code to what i wrote formerly
you can change the following:

ClassA(size_t size_) {

to:
ClassA(size_t size_):StorageP olicy(size_) {

otherwise to:

ClassA(size_t size_):StorageP olicy<T>(size_) {

this will lead to not only removing

default StoragePolicy constructor

from the output but also replacing it with another

Parameter constructor

Nov 5 '06 #2
aaragon wrote:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );

to create an object of ClassA. I would like to change this to

ClassA<VectorPo licy<double ca(6);
Why? The way you handle it currently would allow for specifying a default
policy. With the proposed syntax, you could not do that independent of the
underlying type (e.g., double).
2. The output of this code is as follows:

before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()

before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb

I don't like the idea of instantiating the class with the default
constructor if I'm using only a parameter constructor. Why is this?
If I remove the default constructor then I have a compiler error:
main.cpp: In constructor 'ClassA<T, StoragePolicy>: :ClassA(size_t)
[with T = double, StoragePolicy = BitSetPolicy]':
main.cpp:110: instantiated from here
main.cpp:87: error: no matching function for call to
'BitSetPolicy<d ouble>::BitSetP olicy()'
main.cpp:60: note: candidates are:
BitSetPolicy<T> ::BitSetPolicy( size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<do uble>::BitSetPo licy(const BitSetPolicy<do uble>&)

I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?
I am not quite sure, I understand what you mean. However, the code I provide
below does print default constructor messages.
3. I also tried to include in the policies, a create(size_t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/
Huh?
Maybe some of my questions are too basic, so I apoligize and I
appreciate any help you can give me.

Alejandro Aragón

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset. hpp>

using boost::dynamic_ bitset;

using namespace std;

template <class T>
class StoragePolicy
{
public:

StoragePolicy() ;
StoragePolicy(s ize_t size_);

void print();

};

template <class T>
struct VectorPolicy
{
public:

typedef std::vector<TSt ructure;

VectorPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

VectorPolicy(si ze_t size_){
cout<<"Paramete r constructor"<<e ndl;
str_ = new vector<T>(size_ );
}

void print(){
cout<<"VectorPo licy print()"<<endl;
}

private:

Structure* str_;
};

template <class T>
struct BitSetPolicy
{
public:

typedef boost::dynamic_ bitset<Structur e;

BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

BitSetPolicy(si ze_t size_){
cout<<"Paramete r constructor"<<e ndl;
str_ = new dynamic_bitset< >(size_);
}

void print(){
cout<<"BitSetPo licy print()"<<endl;
}

private:

Structure* str_;
};

template
<
class T,
template <classclass StoragePolicy
>>
class ClassA : public StoragePolicy<T >
{

StoragePolicy<T >* structure_;

public:

// parameter constructor
ClassA(size_t size_) {
structure_ = new StoragePolicy<T >(size_);
}

// print function
void print()
{
cout<<"ClassA print()"<<endl;
structure_->print();
}
};
int main()

{
cout<<"before instantiation of ca"<<endl;
ClassA<double,V ectorPolicyca(6 );
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,B itSetPolicycb(6 );
cout<<"after instantiation of cb"<<endl;
return 0;
}
Get rid of all the duplicates and pointers:

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset. hpp>

using boost::dynamic_ bitset;

using namespace std;
template <class T>
struct VectorPolicy {

typedef std::vector<TSt ructure;

VectorPolicy()
: str_ ()
{
cout<<"default StoragePolicy constructor"<<e ndl;
}

VectorPolicy (size_t size_)
: str_ ( size_ )
{
cout<<"Paramete r constructor"<<e ndl;
}

void print(){
cout<<"VectorPo licy print()"<<endl;
}

private:

Structure str_;

};

template <class T>
struct BitSetPolicy {

typedef boost::dynamic_ bitset<Structur e;

BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

BitSetPolicy(si ze_t size_)
: str_ ( size_ )
{
cout<<"Paramete r constructor"<<e ndl;
}

void print(){
cout<<"BitSetPo licy print()"<<endl;
}

private:

Structure str_;

};

template < class T, template <classclass StoragePolicy >
class ClassA : public StoragePolicy<T {

typedef StoragePolicy<T storage_base;

public:

// parameter constructor
ClassA(size_t size_)
: storage_base ( size_ )
{}

// print function
void print()
{
cout<<"ClassA print()"<<endl;
storage_base::p rint();
}

};
int main(){
cout<<"before instantiation of ca"<<endl;
ClassA<double,V ectorPolicyca(6 );
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,B itSetPolicycb(6 );
cout<<"after instantiation of cb"<<endl;
return 0;
}
Now, you would need to add some methods to your policies that will allow you
to actually manipulate the contents of the underlying container.
Best

Kai-Uwe Bux
Nov 5 '06 #3
"aaragon" <al************ **@gmail.comwro te:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );

to create an object of ClassA. I would like to change this to

ClassA<VectorPo licy<double ca(6);
ClassA both *is* a StoragePolicy and contains one, I doubt this is what
you want but I don't know from the code you present which way to go with
it. The following works with the code presented though.

template < typename StoragePolicy >
class ClassA
{
StoragePolicy structure; // note, no need to 'new' the structure
public:
ClassA( size_t size ): structure( size ) { }

void print() {
cout << "ClassA print()\n";
structure.print ();
}
};

I suggest you read up on "initializa tion lists" as well.
2. The output of this code is as follows:

before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()

before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb

I don't like the idea of instantiating the class with the default
constructor if I'm using only a parameter constructor. Why is this?
Because ClassA derives from it's storage policy and doesn't initialize
the base class object.
I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?
I can't answer the first question, only you know if it's necessary to
instantiate the object twice. But if you don't want to do so, use the
ClassA that I provided above.
3. I also tried to include in the policies, a create(size t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/
Alexandrescu's concepts are quite high level, not something I would
recommend to a person new to the language. Stick to using dynamic
binding.

--
To send me email, put "sheltie" in the subject.
Nov 5 '06 #4
terminator wrote:
aaragon wrote:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.

Dear aaragon:

i have veiwed your code listing .i am affraid
it looks to me that you are no c++ programer .
Indeed, I'm an engineer. However, I found in C++ an amazing tool so
I'm doing my best to learn the language.
>
i do not claim to be professional
and i dont know Alexanderscue either

i did not understand what the program is intended to do
The class is intented to provide several data structures as a simple
option to the user. Then, I will be able to chose for example between
a dynamic bitset or a vector of boolean values. In this way I will be
able to compare performance between data structures.
and i doubt that you can get a program
that satisfies your expectations
but assuming that you know what you are doing
i have the following recommendations for you:

you wrote:
template <class T>
class StoragePolicy
{
public:
StoragePolicy() ;
StoragePolicy(s ize_t size_);
void print();

};

this portion of the code looks meaningless and error prone
remove or comment it out
I agree now so I did what you said.
>
1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );
to create an object of ClassA. I would like to change this to
ClassA<VectorPo licy<double ca(6);


you have defined the class as:

template
<
class T,
template <classclass StoragePolicy

class ClassA : public StoragePolicy<T >
{
this misses a '>' before 'class ClassA' and causes
the compiler not to compile the code, further more
this fraction of the code in its current state
(after adding the afformentioned missing '>')
compiles on microsoft platform but i am not sure that it
is standard and portable to other platforms such as
borland.
Yes, you were right but it was a copy error. I was able to compile the
code before I submitted the code in this post. I'm using SuSE Linux
v10.1 with the g++ compiler (version 4.x).
therefore i agree with you in changing
the code to this:

template
<
class StoragePolicy
class ClassA : public StoragePolicy
{

then you have to type the code as:

ClassA<VectorPo licy<double ca(6);
Yes! Thank you, this is exactly what I wanted. I changed the code.
but in order to have a more consistent code
optionally:
1. you can type
typedef T value_type;
after the first '{'
following 'struct VectorPolicy' and 'struct BitSetPolicy'

2.if and only if you did the first ,type

typedef typename StoragePolicy:: value_type value_type;

after the first '{'
following 'class ClassA'
I don't see the purpose of doing this. What do you mean by more
consistent? I pasted the final version of the code in the end. If you
see the BitSetStorage, there is no point in having a templated
parameter (since the bitset is defined as boost::bitset<> ). However, I
would like to know why you think the code is more consistent (I'm
learning so I appreicate any input).

2. The output of this code is as follows:
before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()
before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb
I don't like the idea of instantiating the class with the default
constructor

you have Writen:

class ClassA : public StoragePolicy<T >

remove or comment out ' : public StoragePolicy<T >'
and the output will not contain:
default StoragePolicy constructor
Indeed, now that I know the right answer it seems obvious. In
Alexandrescu's book (1.10 Customizing Structure with Policy Classes) he
puts "Of course, SmartPtr must either derive from Storage<TOR
aggregate a Storage<Tobject in order to embed the needed structure".
He is taking about a smart pointer class that has a customized
structure through a storage policy class. I realize now that I was
doing both! (I was deriving from the storage class and aggregating an
object as well, and this I don't want). As you suggested, I removed
the inheritance part and now I have only the parameter constructor! =)
alternatively if you have changed the code to what i wrote formerly
you can change the following:

ClassA(size_t size_) {

to:
ClassA(size_t size_):StorageP olicy(size_) {

otherwise to:

ClassA(size_t size_):StorageP olicy<T>(size_) {

this will lead to not only removing

default StoragePolicy constructor

from the output but also replacing it with another

Parameter constructor
About this point, the use of an initialization list isn't the same as
the code that I wrote in the beginning? The code that I have now is as
follows:

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset. hpp>

using boost::dynamic_ bitset;

using namespace std;

template <class T>
struct VectorPolicy
{
public:

typedef std::vector<TSt ructure;

VectorPolicy(si ze_t size_)
: str_(size_) {
cout<<"Paramete r constructor"<<e ndl;
}

void print(){
cout<<"VectorPo licy print()"<<endl;
for(size_t i=0; i<str_.size(); i++)
cout<<str_[i];
}

private:
Structure str_;
};

struct BitSetPolicy
{
public:

typedef boost::dynamic_ bitset<Structur e;

BitSetPolicy(si ze_t size_)
: str_ ( size_ )
{
cout<<"Paramete r constructor"<<e ndl;
}

void print(){
cout<<"BitSetPo licy print()"<<endl;
cout<<str_<<end l;
}

private:

Structure str_;
};

template
<
class StoragePolicy
>
class ClassA
{
StoragePolicy structure_;

public:

// parameter constructor
ClassA(size_t size_)
: structure_(size _)
{
;
}

// print function
void print()
{
cout<<"ClassA print()"<<endl;
structure_.prin t();
}
};

int main()
{
cout<<"before instantiation of ca"<<endl;
ClassA<VectorPo licy<double ca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<BitSetPo licycb(6);
cout<<"after instantiation of cb"<<endl;
cb.print();

return 0;
}

Nov 5 '06 #5
Thanks for replying Mr. Kai-Uwe Bux.

Kai-Uwe Bux wrote:
aaragon wrote:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );

to create an object of ClassA. I would like to change this to

ClassA<VectorPo licy<double ca(6);

Why? The way you handle it currently would allow for specifying a default
policy. With the proposed syntax, you could not do that independent of the
underlying type (e.g., double).
Well, I changed but I can still provide a default policy:

template
<
class StoragePolicy = BitSetPolicy
>
class ClassA;

I added another example within main and it works fine! =)
2. The output of this code is as follows:

before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()

before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb

I don't like the idea of instantiating the class with the default
constructor if I'm using only a parameter constructor. Why is this?
If I remove the default constructor then I have a compiler error:
main.cpp: In constructor 'ClassA<T, StoragePolicy>: :ClassA(size_t)
[with T = double, StoragePolicy = BitSetPolicy]':
main.cpp:110: instantiated from here
main.cpp:87: error: no matching function for call to
'BitSetPolicy<d ouble>::BitSetP olicy()'
main.cpp:60: note: candidates are:
BitSetPolicy<T> ::BitSetPolicy( size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<do uble>::BitSetPo licy(const BitSetPolicy<do uble>&)

I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?

I am not quite sure, I understand what you mean. However, the code I provide
below does print default constructor messages.
I realized that I was inheriting from the base class (something I
didn't need to do). I removed the inheritance part and now I don't
have the default constructor messages.
3. I also tried to include in the policies, a create(size_t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/

Huh?
Maybe some of my questions are too basic, so I apoligize and I
appreciate any help you can give me.

Alejandro Aragón

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset. hpp>

using boost::dynamic_ bitset;

using namespace std;

template <class T>
class StoragePolicy
{
public:

StoragePolicy() ;
StoragePolicy(s ize_t size_);

void print();

};

template <class T>
struct VectorPolicy
{
public:

typedef std::vector<TSt ructure;

VectorPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

VectorPolicy(si ze_t size_){
cout<<"Paramete r constructor"<<e ndl;
str_ = new vector<T>(size_ );
}

void print(){
cout<<"VectorPo licy print()"<<endl;
}

private:

Structure* str_;
};

template <class T>
struct BitSetPolicy
{
public:

typedef boost::dynamic_ bitset<Structur e;

BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

BitSetPolicy(si ze_t size_){
cout<<"Paramete r constructor"<<e ndl;
str_ = new dynamic_bitset< >(size_);
}

void print(){
cout<<"BitSetPo licy print()"<<endl;
}

private:

Structure* str_;
};

template
<
class T,
template <classclass StoragePolicy
>
class ClassA : public StoragePolicy<T >
{

StoragePolicy<T >* structure_;

public:

// parameter constructor
ClassA(size_t size_) {
structure_ = new StoragePolicy<T >(size_);
}

// print function
void print()
{
cout<<"ClassA print()"<<endl;
structure_->print();
}
};
int main()

{
cout<<"before instantiation of ca"<<endl;
ClassA<double,V ectorPolicyca(6 );
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,B itSetPolicycb(6 );
cout<<"after instantiation of cb"<<endl;
return 0;
}

Get rid of all the duplicates and pointers:

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset. hpp>

using boost::dynamic_ bitset;

using namespace std;
template <class T>
struct VectorPolicy {

typedef std::vector<TSt ructure;

VectorPolicy()
: str_ ()
{
cout<<"default StoragePolicy constructor"<<e ndl;
}

VectorPolicy (size_t size_)
: str_ ( size_ )
{
cout<<"Paramete r constructor"<<e ndl;
}

void print(){
cout<<"VectorPo licy print()"<<endl;
}

private:

Structure str_;

};

template <class T>
struct BitSetPolicy {

typedef boost::dynamic_ bitset<Structur e;

BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<e ndl;
}

BitSetPolicy(si ze_t size_)
: str_ ( size_ )
{
cout<<"Paramete r constructor"<<e ndl;
}

void print(){
cout<<"BitSetPo licy print()"<<endl;
}

private:

Structure str_;

};

template < class T, template <classclass StoragePolicy >
class ClassA : public StoragePolicy<T {

typedef StoragePolicy<T storage_base;

public:

// parameter constructor
ClassA(size_t size_)
: storage_base ( size_ )
{}

// print function
void print()
{
cout<<"ClassA print()"<<endl;
storage_base::p rint();
}

};
int main(){
cout<<"before instantiation of ca"<<endl;
ClassA<double,V ectorPolicyca(6 );
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,B itSetPolicycb(6 );
cout<<"after instantiation of cb"<<endl;
return 0;
}
Now, you would need to add some methods to your policies that will allow you
to actually manipulate the contents of the underlying container.
Indeed, this is my next step. Thank you.

Best

Kai-Uwe Bux
Nov 5 '06 #6

Daniel T. wrote:
"aaragon" <al************ **@gmail.comwro te:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
1. In the current version, the user has to type

ClassA<double,V ectorPolicyca(6 );

to create an object of ClassA. I would like to change this to

ClassA<VectorPo licy<double ca(6);

ClassA both *is* a StoragePolicy and contains one, I doubt this is what
you want but I don't know from the code you present which way to go with
it.
This was true and it is not what I wanted. I removed the inheritance
part.
The following works with the code presented though.

template < typename StoragePolicy >
class ClassA
{
StoragePolicy structure; // note, no need to 'new' the structure
public:
ClassA( size_t size ): structure( size ) { }

void print() {
cout << "ClassA print()\n";
structure.print ();
}
};

I suggest you read up on "initializa tion lists" as well.
I understand the concept of initialization lists but I think they are
only useful when you inherit from a base class (in order to call the
constructor of the base class). Now that I removed the inheritance
part of the code and I embedded an object of a Storage policy, I guess
that using an initialization list gives me the same result, right? I
mean:

// parameter constructor
ClassA(size_t size_)
: structure_(size _)
{}

is the same as

// parameter constructor
ClassA(size_t size_)
{
structure_ = StoragePolicy(s ize_);
}

2. The output of this code is as follows:

before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()

before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb

I don't like the idea of instantiating the class with the default
constructor if I'm using only a parameter constructor. Why is this?

Because ClassA derives from it's storage policy and doesn't initialize
the base class object.
I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?

I can't answer the first question, only you know if it's necessary to
instantiate the object twice. But if you don't want to do so, use the
ClassA that I provided above.
3. I also tried to include in the policies, a create(size t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/

Alexandrescu's concepts are quite high level, not something I would
recommend to a person new to the language. Stick to using dynamic
binding.

--
To send me email, put "sheltie" in the subject.
Nov 5 '06 #7
"aaragon" <al************ **@gmail.comwro te:
Daniel T. wrote:
"aaragon" <al************ **@gmail.comwro te:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don't like about the code and that is the
reason I'm posting this message.
1. In the current version, the user has to type
>
ClassA<double,V ectorPolicyca(6 );
>
to create an object of ClassA. I would like to change this to
>
ClassA<VectorPo licy<double ca(6);
ClassA both *is* a StoragePolicy and contains one, I doubt this is what
you want but I don't know from the code you present which way to go with
it.

This was true and it is not what I wanted. I removed the inheritance
part.
The following works with the code presented though.

template < typename StoragePolicy >
class ClassA
{
StoragePolicy structure; // note, no need to 'new' the structure
public:
ClassA( size_t size ): structure( size ) { }

void print() {
cout << "ClassA print()\n";
structure.print ();
}
};

I suggest you read up on "initializa tion lists" as well.

I understand the concept of initialization lists but I think they are
only useful when you inherit from a base class (in order to call the
constructor of the base class).
But if you don't want the policy default constructed before setting its
size, then you have to use an initialization list.
Now that I removed the inheritance
part of the code and I embedded an object of a Storage policy, I guess
that using an initialization list gives me the same result, right? I
mean:

// parameter constructor
ClassA(size_t size_)
: structure_(size _)
{}

is the same as

// parameter constructor
ClassA(size_t size_)
{
structure_ = StoragePolicy(s ize_);
}
THey are different. In the latter case, the structure_ is default
initialized and the copy constructor is called using yet another
StoragePolicy object (that has been initialized using the size_t
constructor) as a parameter. This isn't Java.

--
To send me email, put "sheltie" in the subject.
Nov 5 '06 #8

aaragon wrote:
Daniel T. wrote:
I suggest you read up on "initializa tion lists" as well.

I understand the concept of initialization lists but I think they are
only useful when you inherit from a base class (in order to call the
constructor of the base class).
What about constant data members, reference-type data members or
members for which default construction is impossible or inappropriate?
Now that I removed the inheritance
part of the code and I embedded an object of a Storage policy, I guess
that using an initialization list gives me the same result, right? I
mean:

// parameter constructor
ClassA(size_t size_)
: structure_(size _)
{}
The object structure_ is constructed using the constructor that takes a
single size_t parameter.
is the same as

// parameter constructor
ClassA(size_t size_)
By this time, the object structure_ has been default constructed.
{
structure_ = StoragePolicy(s ize_);
And now you create an unnamed temporary object of type StoragePolicy,
constructed using the constructor that takes a single size_t parameter.
That unnamed temporary object is then assigned to structure_. The
temporary is then destroyed.
}
The two are not the same. The first way constructs structure_ in the
form that you need it. That's it. Done.

The second way default constructs structure_ and then constructs
another object which is then assigned to structure_ before being
destroyed. The second way involves the creation and destruction of a
needless second object, plus a copy-assignment. Any of those may or may
not be costly exercises depending on the class involved. Also, the
second way requires a default constructor and copy-assignment operator
to exist for StoragePolicy, which may or may not be a requirement you
want to add.

So in short, lots of potential negatives and no positives.

Gavin Deane

Nov 5 '06 #9

aaragon wrote:
terminator wrote:
>this misses a '>' before 'class ClassA' and causes
i should apologize for this since it is google`s fault.
>but in order to have a more consistent code
optionally:
1. you can type
typedef T value_type;
after the first '{'
following 'struct VectorPolicy' and 'struct BitSetPolicy'

2.if and only if you did the first ,type
typedef typename StoragePolicy:: value_type value_type;
after the first '{'
following 'class ClassA'


I don't see the purpose of doing this. What do you mean by more
consistent? I pasted the final version of the code in the end. If you
see the BitSetStorage, there is no point in having a templated
parameter (since the bitset is defined as boost::bitset<> ). However, I
would like to know why you think the code is more consistent (I'm
I guessed that you might have been new to OOP and
wrote every thing based on that.I forgot to and not to
write a lot of things.E.G I forgot to write that 'new'
and 'delete' are matching braces in C++.I did not write
that since '<>' was empty the default type parameter would
be used for 'boost::dynamic _bitset<>'.
Since I did not know what you wanted ,again I guessed that
what you posted might not be the ultimate program
you wanted to write.In current version you dont want to deal
with the elements of your storage but if you wanna use them e.g
in a member function then it will need to know the type.
Since the first argument to 'ClassA' was removed some mechanism
seemed essential in order to replace it and I had forgoten
that STL containers (vector,map,... ) define the 'value_type'
inside themselves.Ther efore I included the definition explicitly.
Now that you have removed the inheritance this might help
if you need to do more than just comparing the container types.
Have a look at STL headers(documen tation is better)
and you will get what i mean.

Nov 7 '06 #10

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

Similar topics

6
2640
by: Jason | last post by:
Sorry for the post here but could not find an Exchange newgroup. I developed an application a year or 2 ago that sends SMTP email. The application allows the user to customize the text from address. is "My Custom Name" Jason@Yahoo.com. When the email was received into my companies Exchange 5.5 mail system the custom name was kept intact. Now that
9
514
by: Martin Vorbrodt | last post by:
I'm designing a Matrix class of 4x4 matrices used in computer graphics. Both OpenGL and Direct3D can take a pointer to array of 16 floats which represent the values in the matrix. OGL takes it in column order, D3D in row order. Therefore row = 2, col = 3 will result in different offset into the float array depending on the API. Here is my basic design test program: #include <iostream> using std::cout; using std::endl;
0
1824
by: Oliver Elphick | last post by:
The attached proposal is written primarily for Debian. Its motivation is that the current package upgrade process is pretty flaky and also that the current packaging does not really provide for multiple simultaneous postmasters, which are wanted by people hosting several database clusters. I assume these proposals may also be of interest to Red Hat packagers. I am copying it to the PostgreSQL list in case there are any obvious...
15
38404
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a memcpy(bptr, aptr, sizeof(myStruct));
4
6700
by: VR | last post by:
I am trying to embed a check box into a FlexGrid's cell, but having a problem when I start scrolling the grid. Here is my MyCheckBox class... class MyCheckBox : CheckBox { void Init ( AxMSFlexGridLib.AxMSFlexGrid oGrid,
3
16885
by: Stig Nielsson | last post by:
Hi, I am using a strongly typed dataset in .Net 2.0, and would like all tables to inherit from a cusomt datatable class instead of DataTable. The reason for this is that I want some common code excuted on the row events, and this I want to implement this once in a Table base class. ie. instead of having this code generated:
6
1197
by: zacks | last post by:
I recently wrote a VB.NET application using VS2005. I needed to be able to build linked lists in memory of items that were represented by a group of variables. For example, Friend Structure foo Dim var1 As String Dim var2 As Integer Dim var3 As Boolean End Structure
4
1758
by: aaragon | last post by:
Hello everyone. I'm reading Andrei's Alexandrescu's book on Modern C++ Design and I decided to implemente in my research code the use of policy classes. My objective is to abstract the storage of all individuals in a population. To that purpose, I created a Population class, as follows: template <class T> class CreationPolicy { public:
0
1440
by: er | last post by:
hi, i'm trying to understand A) policy classes and see if they meet my needs. the book "C++ templates" has something similar called B) "bridge pattern implemented with templates". would it be fair to characterize them like this? template<class Impl>//Impl has to have void op(); class A{//bridge pattern Impl impl;
0
9961
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
11178
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10777
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...
0
10438
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
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4243
muto222
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.