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

Compile time check for virtual destructor

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
Jul 22 '05 #1
5 2448
On Sun, 15 Aug 2004 10:23:10 -0400, Kai-Uwe Bux <jk********@gmx.net>
wrote:

[snip]
Is there a standard way of checking for a virtual destructor?


I don't know of one. I tried something, but soon gave up as I
discovered that it seems impossible to take the address of the
destructor (or declare a pointer to member for the destructor).

Seems like a bit of overkill, anyway ... if you need compile-time
checking, I would just look at the base class' declaration. If that
destructor is virtual, all derived destructors are also virtual.

It is perfectly legal to derive a class from a base class which has no
virtual destructor as long as the object is never deleted through a
pointer to the base class (e.g. using private inheritance).
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message news:cf**********@news01.cit.cornell.edu...

Is there a standard way of checking for a virtual destructor?


No, you're about hit it. There is no way to generally test for whether
a specific function (destructor or otherwise) is virtual. As you guessed,
your test for is_polymorphic is also dependent on implementation particulars.

You might be able to fix the general "is_polymorphic" test with some
concoction of dynamic_cast, as it's ill-formed to apply dynamic_cast
in some circumstances to non-polymorphic objects.

Jul 22 '05 #3
Ron Natalie wrote:

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:cf**********@news01.cit.cornell.edu...

Is there a standard way of checking for a virtual destructor?


No, you're about hit it. There is no way to generally test for whether
a specific function (destructor or otherwise) is virtual. As you
guessed, your test for is_polymorphic is also dependent on implementation
particulars.

You might be able to fix the general "is_polymorphic" test with some
concoction of dynamic_cast, as it's ill-formed to apply dynamic_cast
in some circumstances to non-polymorphic objects.

Thanks a lot, that is a great idea:

template < typename T >
class CHECK_is_polymorphic {
private:

struct XXX : public T {}

XXX* xxx ( T* ptr ) {
return( dynamic_cast< XXX* >( ptr ) );
}

public:

CHECK_is_polymorphic ( void ) {
T t;
xxx( &t );
}

};

This seems to do it. The error messages I get from g++ are frightening, and
maybe some improvements are in order; but the basic functionality seems to
be working. I can live with the shortcoming that the destructor still might
not be virtual -- the compiler issues warnings about that anyway.
Thanks again

Kai-Uwe Bux
Jul 22 '05 #4
On Sun, 15 Aug 2004 11:30:33 -0400, Kai-Uwe Bux <jk********@gmx.net>
wrote:
Ron Natalie wrote:

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:cf**********@news01.cit.cornell.edu...

Is there a standard way of checking for a virtual destructor?

No, you're about hit it. There is no way to generally test for whether
a specific function (destructor or otherwise) is virtual. As you
guessed, your test for is_polymorphic is also dependent on implementation
particulars.

You might be able to fix the general "is_polymorphic" test with some
concoction of dynamic_cast, as it's ill-formed to apply dynamic_cast
in some circumstances to non-polymorphic objects.

Thanks a lot, that is a great idea:

template < typename T >
class CHECK_is_polymorphic {
private:

struct XXX : public T {}

XXX* xxx ( T* ptr ) {
return( dynamic_cast< XXX* >( ptr ) );
}

public:

CHECK_is_polymorphic ( void ) {
T t;
xxx( &t );
}


You can simplify that a bit:

template < typename T >
class CHECK_is_polymorphic {
public:

static int const checker =
sizeof(dynamic_cast<void*>(static_cast<T*>(0)));
};

That way you don't need to instantiate the constructor to get the
error.
This seems to do it. The error messages I get from g++ are frightening, and
maybe some improvements are in order; but the basic functionality seems to
be working. I can live with the shortcoming that the destructor still might
not be virtual -- the compiler issues warnings about that anyway.


Boost has a portable version of is_polymorphic in its type traits
library. www.boost.org

Tom
Jul 22 '05 #5
tom_usenet wrote:
template < typename T >
class CHECK_is_polymorphic {
private:

struct XXX : public T {}

XXX* xxx ( T* ptr ) {
return( dynamic_cast< XXX* >( ptr ) );
}

public:

CHECK_is_polymorphic ( void ) {
T t;
xxx( &t );
}
You can simplify that a bit:

template < typename T >
class CHECK_is_polymorphic {
public:

static int const checker =
sizeof(dynamic_cast<void*>(static_cast<T*>(0)));
};

That way you don't need to instantiate the constructor to get the
error.


Thanks a lot,
I just put three lines into my polymorphic_ptr<T> template:

// CONCEPT CHECK: [T has to be polymorphic.]
static unsigned long const
CHECK_IS_POLYMORPHIC = sizeof( dynamic_cast<void*>( static_cast<T*>(0) ) );

And the best part is: the error message is way cleaner.

Boost has a portable version of is_polymorphic in its type traits
library. www.boost.org


I looked that up. It appears that their implementation is very much along
the lines of my first attempt (using sizeof to see whether a vtable gets
added when a virtual destructor is enforced). Since I am just looking for a
way to make the compiler choke on polymorphic_ptr< std::string >, this
seems too elaborate.
Thanks again

Kai-Uwe Bux
Jul 22 '05 #6

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

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
25
by: JKop | last post by:
Using MSWindows as an example: On MSWindows, there's a thing called the System Registry, which is a really big database that holds all the settings of the OS. There's API's for working with the...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
6
by: E. Robert Tisdale | last post by:
The following example shows how an Object Oriented C programmer might implement run-time polymorphism. The Circle "class" is "derived" from the Shape "class". Pointers to functions which actually...
7
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.