https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86491

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
In C++98 entities in an anonymous namespace did not have internal linkage, that
was changed for C++11. But they can still cause ODR violations. So the
diagnostic talks about anonymous namespaces because that's what causes the
potential for ODR violations, not linkage. THat was changed for C++11 so that
anonymous namespacs give internal linkage, and things with internal linkage can
now be used in templates, so the existing warning was extended to cover those
cases, but without rewording it.

In C++98 the testcase in comment 7 is simply ill-formed:

tM.C:3:15: error: ‘& d’ is not a valid template argument of type ‘int*’ in
C++98 because ‘d’ does not have external linkage

But if you change it to use an anonymous namespace it's valid in C++98:

# 1 "t1.H"
template < int *_C_OBJ_> struct NT{};
# 2 "tM.C"
namespace { int d; }
struct D : NT<&d> {};

This is valid in C++98 because 'd' doesn't have internal linkage (it's just
different in every translation unit), but it is still a potential ODR
violation, so there's a warning:

tM.C:3:8: warning: ‘D’ has a base ‘NT<(& {anonymous}::d)>’ whose type uses the
anonymous namespace [-Wsubobject-linkage]


Maybe for C++98 it should always say "anonymous namespace" since that's the
only way to trigger the warning, and for C++11 and later it should say internal
linkage, since that is true for names declared 'static' or in an anon
namespace. That seems like an easy change.

Reply via email to