?
=?windows-1252?Q?Erik_Wikstr=F6m?=
In school (no I will not ask you to do my schoolwork for me) we talked
about policy-based design and got an assignment where we got the a code-
fragment from a stack-implementation. The idea with the code was that
if, when its destructor was called, it still contained any elements it
would run 'delete' on them if they were pointers and do nothing if they
were not.
Our assignment was to reimplement the stack using policies so that the
user could (when instansiating the stack) specify if they wanted
'delete' to be run on pointers or not. My problem is that I did not
fully understand the code given, I know what it does but not why it is
possible to do it like that.
Here is the code given:
#include<deque> // stack’s implementation type
#include<string> // for testing
#include<iostream>
namespace non_generic {
struct Yes {}; // a type, corresponding to true
struct No {}; // a type, corresponding to false
template<typename T>
struct IsPtr {
typedef No Result;
};
template<typename T>
struct IsPtr<T*> { // partial specialization <-- HERE HERE
typedef Yes Result;
};
template<typename T>
class Stack {
public:
~Stack() {
cleanup (typename IsPtr<T>::Result() );
}
void push(T t) { s_.push_back(t); }
private:
void cleanup ( Yes ) {
for (I i( s_.begin()); i != s_.end(); ++i)
{
std::cout << "Delete " << **i << "\n";
delete *i;
}
}
void cleanup ( No ) {
for (I i( s_.begin()); i != s_.end(); ++i)
std::cout << "Do nothing for " << *i << "\n";
}
typedef std::deque<T> C;
typedef typename C::iterator I;
C s_;
};
}
The part I don't understand is the one marked with HERE HERE above. What
it does is that if the type T is a pointer the second struct (the one
marked) will be used and hence 'cleanup(Yes)' will be called while if
type T is not a pointer the first struct will be used and 'cleanup(No)'
will be used.
Now, the way I understand things if type T on the line above HERE HER is
a pointer to something, then the T* on the next line (marked HERE HERE)
should correspond to a pointer to a pointer. But somehow that partial
specialisation is used when T is pointer, and that's the part I don't
understand.
My second question is if there's some other more elegant way (I don't
think the one above is elegant) to determine if the type given is a
pointer or not.
about policy-based design and got an assignment where we got the a code-
fragment from a stack-implementation. The idea with the code was that
if, when its destructor was called, it still contained any elements it
would run 'delete' on them if they were pointers and do nothing if they
were not.
Our assignment was to reimplement the stack using policies so that the
user could (when instansiating the stack) specify if they wanted
'delete' to be run on pointers or not. My problem is that I did not
fully understand the code given, I know what it does but not why it is
possible to do it like that.
Here is the code given:
#include<deque> // stack’s implementation type
#include<string> // for testing
#include<iostream>
namespace non_generic {
struct Yes {}; // a type, corresponding to true
struct No {}; // a type, corresponding to false
template<typename T>
struct IsPtr {
typedef No Result;
};
template<typename T>
struct IsPtr<T*> { // partial specialization <-- HERE HERE
typedef Yes Result;
};
template<typename T>
class Stack {
public:
~Stack() {
cleanup (typename IsPtr<T>::Result() );
}
void push(T t) { s_.push_back(t); }
private:
void cleanup ( Yes ) {
for (I i( s_.begin()); i != s_.end(); ++i)
{
std::cout << "Delete " << **i << "\n";
delete *i;
}
}
void cleanup ( No ) {
for (I i( s_.begin()); i != s_.end(); ++i)
std::cout << "Do nothing for " << *i << "\n";
}
typedef std::deque<T> C;
typedef typename C::iterator I;
C s_;
};
}
The part I don't understand is the one marked with HERE HERE above. What
it does is that if the type T is a pointer the second struct (the one
marked) will be used and hence 'cleanup(Yes)' will be called while if
type T is not a pointer the first struct will be used and 'cleanup(No)'
will be used.
Now, the way I understand things if type T on the line above HERE HER is
a pointer to something, then the T* on the next line (marked HERE HERE)
should correspond to a pointer to a pointer. But somehow that partial
specialisation is used when T is pointer, and that's the part I don't
understand.
My second question is if there's some other more elegant way (I don't
think the one above is elegant) to determine if the type given is a
pointer or not.