Customizing structure with policy classes

A

aaragon

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,VectorPolicy> ca(6);

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

ClassA<VectorPolicy<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<double>::BitSetPolicy()'
main.cpp:60: note: candidates are:
BitSetPolicy<T>::BitSetPolicy(size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<double>::BitSetPolicy(const BitSetPolicy<double>&)

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(size_t size_);

void print();

};

template <class T>
struct VectorPolicy
{
public:

typedef std::vector<T> Structure;

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

VectorPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new vector<T>(size_);
}

void print(){
cout<<"VectorPolicy print()"<<endl;
}

private:

Structure* str_;
};

template <class T>
struct BitSetPolicy
{
public:

typedef boost::dynamic_bitset<> Structure;

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

BitSetPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new dynamic_bitset<>(size_);
}

void print(){
cout<<"BitSetPolicy print()"<<endl;
}

private:

Structure* str_;
};

template
<
class T,
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,VectorPolicy> ca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();

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


return 0;
}
 
T

terminator

aaragon said:
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:
template <class T>
class StoragePolicy
{
public:


StoragePolicy();
StoragePolicy(size_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,VectorPolicy> ca(6);


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


ClassA<VectorPolicy<double> > ca(6);


you have defined the class as:

template
<
class T,
template <class> class 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 StoragePolicyclass ClassA : public StoragePolicy
{

then you have to type the code as:

ClassA<VectorPolicy<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_):StoragePolicy(size_) {

otherwise to:

ClassA(size_t size_):StoragePolicy<T>(size_) {

this will lead to not only removing

default StoragePolicy constructor

from the output but also replacing it with another

Parameter constructor
 
K

Kai-Uwe Bux

aaragon said:
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,VectorPolicy> ca(6);

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

ClassA<VectorPolicy<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<double>::BitSetPolicy()'
main.cpp:60: note: candidates are:
BitSetPolicy<T>::BitSetPolicy(size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<double>::BitSetPolicy(const BitSetPolicy<double>&)

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(size_t size_);

void print();

};

template <class T>
struct VectorPolicy
{
public:

typedef std::vector<T> Structure;

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

VectorPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new vector<T>(size_);
}

void print(){
cout<<"VectorPolicy print()"<<endl;
}

private:

Structure* str_;
};

template <class T>
struct BitSetPolicy
{
public:

typedef boost::dynamic_bitset<> Structure;

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

BitSetPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new dynamic_bitset<>(size_);
}

void print(){
cout<<"BitSetPolicy print()"<<endl;
}

private:

Structure* str_;
};

template
<
class T,

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,VectorPolicy> ca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicy> cb(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<T> Structure;

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

VectorPolicy (size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}

void print(){
cout<<"VectorPolicy print()"<<endl;
}

private:

Structure str_;

};

template <class T>
struct BitSetPolicy {

typedef boost::dynamic_bitset<> Structure;

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

BitSetPolicy(size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}

void print(){
cout<<"BitSetPolicy print()"<<endl;
}

private:

Structure str_;

};

template < class T, template <class> class 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::print();
}

};


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

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicy> cb(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
 
D

Daniel T.

aaragon said:
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,VectorPolicy> ca(6);

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

ClassA<VectorPolicy<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 "initialization 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.
 
A

aaragon

terminator said:
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:


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

I agree now so I did what you said.
you have defined the class as:

template
<
class T,
template <class> class 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<VectorPolicy<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).
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<T> OR
aggregate a Storage<T> object 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_):StoragePolicy(size_) {

otherwise to:

ClassA(size_t size_):StoragePolicy<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<T> Structure;

VectorPolicy(size_t size_)
: str_(size_) {
cout<<"Parameter constructor"<<endl;
}

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

private:
Structure str_;
};

struct BitSetPolicy
{
public:

typedef boost::dynamic_bitset<> Structure;

BitSetPolicy(size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}

void print(){
cout<<"BitSetPolicy print()"<<endl;
cout<<str_<<endl;
}

private:

Structure str_;
};

template
<
class StoragePolicyclass ClassA
{
StoragePolicy structure_;

public:

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

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

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

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

return 0;
}
 
A

aaragon

Thanks for replying Mr. Kai-Uwe Bux.

Kai-Uwe Bux said:
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 = BitSetPolicyclass 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<double>::BitSetPolicy()'
main.cpp:60: note: candidates are:
BitSetPolicy<T>::BitSetPolicy(size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<double>::BitSetPolicy(const BitSetPolicy<double>&)

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.
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<T> Structure;

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

VectorPolicy (size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}

void print(){
cout<<"VectorPolicy print()"<<endl;
}

private:

Structure str_;

};

template <class T>
struct BitSetPolicy {

typedef boost::dynamic_bitset<> Structure;

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

BitSetPolicy(size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}

void print(){
cout<<"BitSetPolicy print()"<<endl;
}

private:

Structure str_;

};

template < class T, template <class> class 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::print();
}

};


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

cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicy> cb(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.
 
A

aaragon

Daniel said:
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 "initialization 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(size_);
}
 
D

Daniel T.

aaragon said:
This was true and it is not what I wanted. I removed the inheritance
part.


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(size_);
}

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.
 
G

Gavin Deane

aaragon said:
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(size_);

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
 
T

terminator

i should apologize for this since it is google`s fault.
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.Therefore 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(documentation is better)
and you will get what i mean.
 
A

aaragon

terminator said:
i should apologize for this since it is google`s fault.


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<>'.

Yes, I knew this already.
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.Therefore 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(documentation is better)
and you will get what i mean.

Could you please expand on this? I read the declaration of the
standard vector class but I still don't understand what you say.
Actually, I need to do more than compare containters, I also need to
manipulate the items sometimes.
 
T

terminator

standard vector class but I still don't understand what you say.
Actually, I need to do more than compare containters, I also need to
manipulate the items sometimes.- Hide quoted text -- Show quoted text -

the declaration(!=definition) of 'ClassA' is as follows:
template <class T> class ClassA;
but it is to be used like this:
ClassA < some_container < some_type > > some_variable_name;
in this case the only way to know the o f elements to be stored in
'ClassA' is throghout the 'T' template parameter
therefore the 'T' type should have introduced the type of elelements
to be stored in someway :the best is using a public 'typedef'
instruction in
any class passed to 'ClassA' template as an argument.moreover
what i previously have written forces 'ClassA' to accept parameters
that do define 'value_type' hence resembling STL containers.if you code
as I said you can write something like this:
template <class T> class ClassA{
//just as before:
........
// define index operator:
value_type operator[](unsigned int index){
//some code here
//return an object of 'value_type'
};
};

void main(void){
//declare an instance of 'ClassA':
ClassA < some_container < some_type > > aVar;
//declare an instance of 'some_type':
some_type x;
x=aVar[1];// index 'aVar' with an 'unsigned int'(1 here) and
assign it to 'x'
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top