16

Is there a g++ equivalent to Visual Studio's __declspec(novtable) argument?

Basically, in a pure virtual base class the __declspec(novtable) argument can be used to suppress the creation of a vtable for the base class as well as vtable initialization/deinitialization code in the contstructor/destructor respectively. E.g.,

class __declspec(novtable) PureVirtualBaseClass
{
    public: 
       PureVirtualBaseClass(){}
       virtual ~PureVirtualBaseClass() = 0;
};

See Paul DiLascia's article for more info. Also see my related question.

Community
  • 1
  • 1
oz10
  • 153,307
  • 27
  • 93
  • 128
  • Just curious: It seems to me that discarding the vtable for PureVirtualBaseClass is a microoptimization, and generally a very small one at that. What's the reason for wanting to do this? – Managu Nov 24 '09 at 05:19
  • Read DiLascia's article, he covers the reasons for wanting to do this better than I can. – oz10 Nov 24 '09 at 14:18
  • Also, found this http://msdn.microsoft.com/en-us/library/k13k85ky.aspx today on MSDN where they suggest there can be a significant reduction in program size using __declspec(novtable). – oz10 Nov 26 '09 at 02:35
  • 1
    Wouldn't whole program optimization let the compiler remove the vtables for any classes that aren't instantiated anyway? – Dirk Holsopple Sep 05 '12 at 14:10

1 Answers1

9

I don't think there is one -- if there was, it would be listed under the type attributes page of the GCC manual. GCC uses type attributes to add extra annotations to types (such as alignment and padding), but there is no type attribute equivalent to __declspc(novtable) listed there.

I also don't see any compiler flag in the command line options relating to this optimization.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • It's been a while. Does this answer still apply? – Quest Dec 25 '18 at 15:49
  • I believe nowadays gcc has the -fdevirtualize switch which is in the -O2 optimization – Dave Jun 14 '19 at 02:36
  • @Dave No, according to the documentation on `-fdevirtualize` https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fdevirtualize , that is a completely different optimization. – Adam Rosenfield Jun 14 '19 at 14:53