nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
934 stars 134 forks source link

Empty base class optimization for MSVC #305

Open andipeer opened 1 year ago

andipeer commented 1 year ago

Please include the following information in your issue:

  1. Which version of units you are using 3.0.0 Beta 2

  2. Which compiler exhibited the problem (including compiler version) MSVC

Problem description

In contrast to most other compilers, MSVC does not enable empty base class optimization by default. This means that when using units on MSVC, although most method calls will be optimized away, there will be a memory overhead of at least 1 byte compared to just using plain floating point types.

Solution

As a solution, in our code base we added the empty_bases attribute to the units::unit class in case it is compiled on MSVC. This indeed removes the memory overhead.

If you approve that this should be addressed, I can create a pull request with the required changes.