SafeInt: `static inline` functions are not supported by header units & modules

Would it be possible to make SafeIntExceptionAssert simply inline once again?

Here’s the error as seen in msvc

safeint3.hpp(430,20): error C2129: static function ‘void SafeIntExceptionAssert(void) noexcept’ declared but not defined

About this issue

  • Original URL
  • State: open
  • Created 4 months ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Aha! Changing the safeint type to unsigned int did the trick:

C:\Users\jacobl\source\repos\SafeInt>type example.cpp
import "SafeInt.hpp";

int main(int argc)
{
    SafeInt<unsigned int> x(argc);
    return x;
}
C:\Users\jacobl\source\repos\SafeInt>cl /std:c++latest /nologo /Wall /wd4668 /EHsc /exportHeader SafeInt.hpp
SafeInt.hpp

C:\Users\jacobl\source\repos\SafeInt>cl /std:c++latest /nologo /Wall /wd4668 /EHsc /headerUnit SafeInt.hpp=SafeInt.hpp.ifc example.cpp
example.cpp
C:\Users\jacobl\source\repos\SafeInt\SafeInt.hpp(425): error C2129: static function 'void SafeIntExceptionAssert(void) noexcept' declared but not defined
C:\Users\jacobl\source\repos\SafeInt\SafeInt.hpp(425): note: see declaration of 'SafeIntExceptionAssert'

The compiler is not required to issue a diagnostic for internal linkage names within a header unit, however using such an internal linkage name is ill-formed as per the standard: [basic.link]/2.3. Since the header unit is its own translation unit, the internal linkage function SafeIntExceptionAssert is not usable from outside.

The best way to remove this restriction is to make the function inline, which would have the same effect as marking it static, but also allow the linker to deduplicate instances as well which could decrease binary sizes.