Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert that a particular class does not have any vtbl pointers in its layout?

Tags:

c++

I have a simple RAII class to ensure some handle is properly disposed of. Now I would like to assert that no one is going to add by accident any virtual methods to it. The way I see it, I need to assert that the class does not have the vtbl pointer.

How can I do it? Is it possible to assert at the compile time?

EDIT

I will settle for desktop compilers. As far as I know there are no desktop c++ compilers that are not using vtbl for implementing polymorphism.

like image 315
mark Avatar asked Jan 10 '12 13:01

mark


1 Answers

If you have a C++11 library, you can use std::is_polymorphic<T>:

If T is a polymorphic class (that is, a class that declares or inherits at least one virtual function), provides the member constant value equal true. For any other type, value is false.

like image 131
Some programmer dude Avatar answered Sep 25 '22 01:09

Some programmer dude