1

I have a tiny header file in part of my project which defines a helper function like the following:

static std::string GetErrorString(const MyObj& obj)
{
    // ... return the string
}

If I include this header in a source file where the function is not used, I get the following warning from MSVC:

warning C4505: 'GetErrorString': unreferenced local function has been removed

However, if I change the function to static inline, the warning disappears. I have a couple of questions regarding this:

  1. The function still isn't used. Why does it being inline actually matter?
  2. My understanding was that the inline keyword was merely a suggestion to the compiler, which in its infinite wisdom it is free to ignore. If this is the case, this implies that inline isn't actually the solution to this problem, because if the compiler decided not to inline the function then in theory I'd get the warning back again. What actually is the proper solution?
NoodleCollie
  • 855
  • 7
  • 24
  • Why did you declare it as `static`, I wonder? Do you know what effect that has? – Ulrich Eckhardt Oct 01 '21 at 08:14
  • Side note: A static declared function is only valid for the compilation unit. Meaning this function is compiled into each unit which includes the header instead of linking to this function – RoQuOTriX Oct 01 '21 at 08:15
  • *"My understanding was that the `inline` keyword"*. It is wrong, `inline` keyword is just for ODR, and has nothing to do with inlining (even if it might be a good candidate). – Jarod42 Oct 01 '21 at 08:20
  • I'm closing this as a dupe for a similar question about GCC's behavior. How to implement warnings for families such as this one is entirely up to the compiler vendor, and there have been various discussions over the years regarding particularly whether or not to diagnose static inline functions in headers as unused. The answer to the dupe target gives on rationale as for why this may not be a good idea, but in the end it's a preferential choice of the compiler vendors. – dfrib Oct 01 '21 at 08:30
  • I used `static` because then the function is local to each compilation unit it's included in, correct? Otherwise I get multiple definition linker errors. I must admit my understanding of `inline` comes directly from old C, so perhaps the premise of this question wasn't entirely correct. https://stackoverflow.com/a/1759575/2054335 has been very helpful. – NoodleCollie Oct 01 '21 at 08:47

0 Answers0