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

--- Comment #4 from Jason Vas Dias <jason.vas.dias at gmail dot com> ---
Aha! It is simply that the object pointer template parameter cannot
have static (translation unit) linkage here:

  namespace NA 
  {  class C { ... };
     static C c_;
   /*^^^^^^*/
  }

If I remove the 'static', no warning is generated .

This -Wsubobject-linkage warning is therefore doubly confusing !

Since I am instantiating an object whose symbol has automatic storage class,
('d' in main()), 
why should gcc complain that I have an object containing a static reference ?

And why does it have anything to do with -Wsubobject-linkage or use of 
anonymous namespaces ?

There really is no anonymous namespace usage going on in this code.

I really do want all users of the 'NT' template to get a pointer to the same
global 'class C' 'c_' object , which is ONLY defined in the one place, 
but multiple files must be able to include the same 't2.h' header .

I see now I should have used 'extern C c_', and defined it in tM.C.

But actually, in the context in which it was used, since there was no
other defining translation unit, and it was only instantiated in a main()
program, I do not think the warning should have been issued. If I was
actually trying to instantiate multiple 'class D' objects from multiple
translation units, there would be a problem, but I was not.

And really, that '-Wsubobject-linkage' should be split into:
  A) Detect genuine anonymous namespace use
  B) Detect usage of static object references in headers that can be
     included by multiple files, and issue a separate warning message like 
      'static object reference may not be to same object\
       if used in multiple translation units'
     or something like that.

It is highly confusing to claim that code uses anonymous namespaces 
when it does not.

Reply via email to