top button
Flag Notify
Site Registration

Potentially bogus warning about anonymous types

0 votes
297 views

A while ago I encountered what I believed to be a bogus warning of the form "... uses the anonymous namespace" with GCC 4.8.

I posted http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54060 , and I was told there that:
1. The warning is meant to say "... uses an anonymous type" rather than "... uses the anonymous namespace".
2. Aside from the poor wording, the warning is not bogus because it points out a potential ODR violation.

I recently encountered the same warning in a different situation, but I don't see where the potential ODR violation is this time. I suspect there may not be one (and thus the warning really is bogus in this case), but I wanted to double-check before filing a bug.

Here is the code in question:

test.cpp:

#include "test.hpp"

test.hpp:

template 
struct base
{
    static const bool value = true;
};

template 
struct outer;

template 
struct outer
{
    template  
    struct inner : public base
    {
    };
};

bool foo()
{
    auto lambda = [](){};
    typedef decltype(lambda) Lambda;
    return outer::inner::value;
}

When compiling test.cpp, I get the following warning:

In file included from test.cpp:1:0:
test.hpp: In instantiation of 'struct outer::inner':
test.hpp:23:39:   required from here
test.hpp:14:12: warning: 'outer::inner' has a base 'base' whose type uses the anonymous namespace [enabled by default]
     struct inner : public base

Could someone please confirm whether there is an ODR violation here, and if so explain what it is?

posted May 16, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

There is if you include test.hpp in multiple translation units (and the compiler has to assume you will) because foo() is not declared inline. Do that and the warning goes away.

answer May 16, 2013 by anonymous
I see. Is there something about a nested class, template specialization, or a base class that causes that example to be an ODR violation but not this one (which GCC does not warn about):

template
struct inner
{
    static const bool value = true;
};

bool foo()
{
    auto lambda = [](){};
    typedef decltype(lambda) Lambda;
    return inner::value;
}
Similar Questions
+1 vote

I have an oversight in my code where I'm declaring & defining a function
with C-linkage, though it's not possible.

Example snippet:

#ifdef __cplusplus
extern "C"
{
#endif

// ... some functions which are compatible with C linkage

// Intended to be a helper function not exposed from library
std::string GetEngineVersion()
{
 // ...
}

#ifdef __cplusplus
}
#endif

Obviously the GetEngineVersion function cannot have C linkage because it returns a C++ class.

My question is: does GCC have a warning for this scenario? Specifically, can it warn when something is declared extern "C" that's incompatible with C linkage?

I compile with the following warning flags and I didn't get a warning:

-Wall -Wunused-parameter -Wextra -Weffc++ -Wctor-dtor-privacy
-Wnon-virtual-dtor -Wreorder -Wold-style-cast -Woverloaded-virtual
-Werror

Incidentally, when I compile the same code in VS2013 I get this warning:

warning C4190: 'GetEngineVersion' has C-linkage specified, but returns UDT 'std::basic_string' which is incompatible with C
+1 vote

I don't really understand this option. I enabled it but it warns for essentially all of my classes which have any virtual methods. For
example if I have Foo.h:

 class Foo {
 virtual bool bar();
 ...
 };

then Foo.cpp which implements Foo. And then in another header somewhere else I inherit from Foo and implement bar() differently (as a virtual function). However when GCC compiles Foo.cpp it can't see the other header, so I guess it assumes no one inherits from Foo and suggests it should be marked final:

Declaring type 'class Foo' final would enable devirtualization of N calls

I mean sure, it would, but that would defeat the entire point (as I understand it).

I don't really understand how/where this option is meant to be used.
Ditto for -Wsuggest-final-methods.

0 votes

I'm studying about C compiler for increasing software quality. So I want to get all of compile error message list of gcc about C language. I was trying to find it. But I can't find it anywhere. How can I find it?

Please help?

...