When using Visual C++ 2012 to compile the follow code:
namespace
{
void unusedFunction1()
{
}
}
static void unusedFunction2()
{
}
With /Wall, the compiler reports
warning C4505: 'unusedFunction2' : unreferenced local function has been removed
for the static function unusedFunction2(). But it doesn't report anything for unusedFunction1().
It seems that including local function in the anonymous namespace suppresses the unreferenced local function warning, which is an unexpected and unpleasant side-effect for me.
Is there any method to generate warnings for unreferenced local function in anonymous namespace, either with MSVC or other C++ compilers?
Depending on which C++ standard you're reading, it may contain this note in section 7.3.1 on unnamed namespaces:
"Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit." (eg. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf).
This implies that it is possible for the compiler to produce anonymous namespace symbols with external linkage. Inspecting the .obj file, this is in fact what MSVC is doing with 'unusedFunction1':
Because the function has external linkage, the compiler can't know whether it is used by another compilation unit or not, and thus doesn't throw the warning. It appears other compilers make a better choice about using internal linkage.