How do I generate a warning for an unaccepted local function in an anonymous namespace?

When using Visual C ++ 2012 to compile the following code:

namespace
{
    void unusedFunction1()
    {
    }
}

static void unusedFunction2()
{
}

      

With / Wall, the compiler reports

warning C4505: 'unusedFunction2': unreferenced local function removed

for the static function unusedFunction2 (). But it doesn't report anything about unusedFunction1 ().

It looks like putting a local function in an anonymous namespace suppresses the unpublished local function warning, which is an unexpected and frustrating side effect to me.

Is there any method for generating unbound local function warnings in an anonymous namespace, either with MSVC or other C ++ compilers?

+3


source to share


1 answer


Depending on which C ++ standard you are reading, it may contain this note in section 7.3.1 in unnamed namespaces:

"Although objects in an unnamed namespace may be externally related, they are effectively qualified by a name that is unique to their translation unit and therefore can never be seen with any other translation unit." (e.g. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf ).

This means that the compiler can generate anonymous externally linked namespace symbols. Checking the .obj file, this is actually what MSVC does with unusedFunction1:



dumpbin /symbols a.obj | findstr "unusedFunction"
1345 00003B20 SECT1B notype ()    External     | ?unusedFunction1@?A0xd43c5f4a@@YAXXZ (void __cdecl `anonymous namespace'::unusedFunction1(void))

      

Since the function is linked externally, the compiler cannot know if it is being used by another compilation unit or not, and thus does not issue a warning. It looks like other compilers make better choices in using internal linking.

+2


source







All Articles