Why constexpr functions are also inline?

156 Views Asked by At

I was wondering why qualifying a function as constexpr implies that it must also be inline.

1

There are 1 best solutions below

0
anatolyg On

I don't think there's a good reason, it just happens to be the case. inline does two things: it's an optimization hint, and it removes linker errors on duplicate definitions. The latter is necessary for constexpr, as the function definitions need to be visible in each TU that calls them at compile-time. The former isn't necessary. Templates, for example, also don't error on duplicate definitions, but are not implicitly inline for optimization purposes.

– HolyBlackCat

source