K
Kai-Uwe Bux
Hi,
I decided to abandon the direct use of raw pointers in my programs and
intend to replace them with templates like:
dyn_array_of < typename T, typename Allocator >
pointer_to < typename T, typename Allocator >
polymorphic_ptr < typename T, typename Allocator >
I want pointer_to<T> to not allow for polymorphism whereas
polymorphic_ptr<T> should allow for this. Now, I would like
polymorphic_ptr<T> to perform a compile time check whether T has a virtual
destructor so that the compiler would choke on any atempt to use something
like polymorphic_ptr< std::string >.
Unfortunately, I do not see how I can do this. Here is a work around that
almost does it with gcc-3.4:
template < unsigned long M, unsigned long N >
class CHECK_is_equal {
private:
template < unsigned long A >
class XXX {
public:
void operator== ( const XXX & ) const {};
};
public:
CHECK_is_equal ( void ) {
XXX< M > a;
XXX< N > b;
a == b;
}
};
template < typename T >
class CHECK_is_polymorphic {
private:
class NonVirtual : public T {
public:
~NonVirtual ( void ) {}
};
class Virtual : public T {
public:
virtual ~Virtual ( void ) {}
};
public:
CHECK_is_polymorphic ( void ) {
CHECK_is_equal< sizeof( NonVirtual ), sizeof( Virtual ) > x;
}
};
struct BaseA {
virtual void f ( void ) {};
virtual ~BaseA ( void ) {}
};
struct BaseB {
virtual void f ( void ) {};
~BaseB ( void ) {}
};
struct BaseC {
void f ( void ) {};
virtual ~BaseC ( void ) {}
};
struct BaseD {
void f ( void ) {};
~BaseD ( void ) {}
};
int main ( void ) {
CHECK_is_polymorphic< BaseA > a;
CHECK_is_polymorphic< BaseB > b;
CHECK_is_polymorphic< BaseC > c;
CHECK_is_polymorphic< BaseD > d; // this line gives an error.
}
This does not check for a virtual destructor but for the existence of a
virtual function. Moreover, it depends on the implementation specific
feature that the pointer to the table of virtual function causes the type
size to increase.
Is there a standard way of checking for a virtual destructor?
Best
Kai-Uwe Bux
I decided to abandon the direct use of raw pointers in my programs and
intend to replace them with templates like:
dyn_array_of < typename T, typename Allocator >
pointer_to < typename T, typename Allocator >
polymorphic_ptr < typename T, typename Allocator >
I want pointer_to<T> to not allow for polymorphism whereas
polymorphic_ptr<T> should allow for this. Now, I would like
polymorphic_ptr<T> to perform a compile time check whether T has a virtual
destructor so that the compiler would choke on any atempt to use something
like polymorphic_ptr< std::string >.
Unfortunately, I do not see how I can do this. Here is a work around that
almost does it with gcc-3.4:
template < unsigned long M, unsigned long N >
class CHECK_is_equal {
private:
template < unsigned long A >
class XXX {
public:
void operator== ( const XXX & ) const {};
};
public:
CHECK_is_equal ( void ) {
XXX< M > a;
XXX< N > b;
a == b;
}
};
template < typename T >
class CHECK_is_polymorphic {
private:
class NonVirtual : public T {
public:
~NonVirtual ( void ) {}
};
class Virtual : public T {
public:
virtual ~Virtual ( void ) {}
};
public:
CHECK_is_polymorphic ( void ) {
CHECK_is_equal< sizeof( NonVirtual ), sizeof( Virtual ) > x;
}
};
struct BaseA {
virtual void f ( void ) {};
virtual ~BaseA ( void ) {}
};
struct BaseB {
virtual void f ( void ) {};
~BaseB ( void ) {}
};
struct BaseC {
void f ( void ) {};
virtual ~BaseC ( void ) {}
};
struct BaseD {
void f ( void ) {};
~BaseD ( void ) {}
};
int main ( void ) {
CHECK_is_polymorphic< BaseA > a;
CHECK_is_polymorphic< BaseB > b;
CHECK_is_polymorphic< BaseC > c;
CHECK_is_polymorphic< BaseD > d; // this line gives an error.
}
This does not check for a virtual destructor but for the existence of a
virtual function. Moreover, it depends on the implementation specific
feature that the pointer to the table of virtual function causes the type
size to increase.
Is there a standard way of checking for a virtual destructor?
Best
Kai-Uwe Bux