简体   繁体   中英

Is there a way to decrease the size of the object by removing "vptr"

I have a code like this using CRTP and C++20:

template<clsas Derived>
class Base {
public:

    void m() {
        static_cast<Derived*>(this)->feature();      
    }
   
    virtual constexpr void feature() = 0;

}

class BaseImpl: public Base<BaseImpl> {
     virtual constexpr void feature() final override  { // ... };
}
  • Is there a way to remove "vptr" so the object won't take 8 bytes (for x64) and instead will take only 1 byte? (since it never used with runtime polymorphism)

In real code, the hierarchy is much more complex and it has event 2 vptr (so it takes 16 bytes). Is there any extensions like for GCC or MSVC?

Yeah, definitely, one of the solutions is just to remove the virtual method. But since it has a complex hierarchy it brings extremely ugly errors and unmaintainable code, since programmers should like guess what methods must be implemented via reading unreadable template instantiating errors.

The whole goal is to achieve java like hierarchy (with interfaces,override checks), but with zero-cost abstraction.

I did some experiments (exploring code disassembly) and the compiler completely optimized all the virtual calls, providing zero-cost abstraction. EXCEPT it extends the object to have "vptr" which is never used in my code.

I found something like "__declspec(novtable)" but "vptr" still takes space. And yes, the size of the objects is extremely important to be as less as possible.

You're using CRTP, which uses static dispatch. There's absolutely no reason to use virtual here. If you want to ensure that the method exists with the right signature, use a static_assert .

template<class Derived>
class Base {
public:

    void m() {
        static_cast<Derived*>(this)->feature();      
    }
    
    ~Base() {
        static_assert(std::is_same_v<void,decltype(static_cast<Derived*>(this)->feature())>);
    }
};

class BaseImpl: public Base<BaseImpl> {
public:
     constexpr void feature()  {  };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM