Bug 54060 - G++ warning mixes up anonymous types and anonymous namespaces
Summary: G++ warning mixes up anonymous types and anonymous namespaces
Status: RESOLVED DUPLICATE of bug 53184
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2012-07-21 06:55 UTC by Nathan Ridge
Modified: 2013-06-15 18:16 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-07-31 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Ridge 2012-07-21 06:55:39 UTC
The following code, when in a header file:

auto L = [](){};
struct S
{
    decltype(L) m;
};

Produces the following warning:

test.hpp:3:8: warning: 'S' has a field 'S::m' whose type uses the anonymous namespace [enabled by default]
 struct S
        ^

This warning suggests that the type of the lambda is in an anonymous namespace. 

A similar warning is given if L is declared at namespace or class scope:

namespace N
{
    auto L = [](){};
}
struct S
{
    decltype(N::L) m;
};


class N
{
    static constexpr auto L = [](){};
};
struct S
{
    decltype(N::L) m;
};


According to section 5.1.2/3 of the Standard,

"The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression."

This suggests that the types of the lambdas in the above examples should not be in an anonymous namespace, because that would not be the smallest scope that contains the lambda-expression.
Comment 1 Jason Merrill 2012-07-31 19:15:21 UTC
The lambda isn't really in the anonymous namespace, the warning message is misleading.  This also happens for non-lambda anonymous types, such as

typedef struct { } *A;

struct B
{
  A a;
};
Comment 2 Nathan Ridge 2012-07-31 20:21:51 UTC
What is the purpose of warning about the use of an anonymous type in this context?
Comment 3 Jonathan Wakely 2012-07-31 20:41:51 UTC
Every file that includes the code gets a different type, so S (in comment 0) and B (in comment 1) violate the ODR if defined in more than one translation unit. That's why the warning is given when the code occurs in a header.
Comment 4 Paolo Carlini 2013-06-15 18:16:23 UTC
Same issue, right? And it seems easy to fix, we have only to reword a bit the message.

*** This bug has been marked as a duplicate of bug 53184 ***