Re: Synopsis Question

David Abrahams <[email protected]>
Newsgroups gmane.comp.documentation.synopsis
Message-ID <[email protected]>
Stefan Seefeld <[email protected]> writes:

> David Abrahams wrote:
>
>>>Huh ? Isn't an anonymous (or 'unnamed' as per the spec 7.3.1.1) namespace
>>> specifically provided to avoid accidental ODR violations, 
>> Well, if you use it in source (.cpp) files, yeah.  But then the
>> symbol
>> is hidden from other TUs because, well, it's not *present* in those
>> TUs.  
>
> It could, without the anonymous namespace, still be referenced ('forward declared')
> as 'extern', and the linker would resolve it. With anonymous namespaces the
> linker will complain.

Yes.  I can't envision how you'd create that secenario in a useful
program, though.

>>>i.e. to make symbols local to the file containing that anonymous
>>>namespace ?
>> Yeah, but that doesn't prevent ODR violations by itself.  If you use
>> the unnamed namespace in a header it's as likely as not to *cause* ODR
>> violations, indirectly, when a template uses the symbols.
>
> You are right. I'v never used it in headers, and in fact, I don't quite
> understand what the purpose would be to put it into a header (see
> http://synopsis.fresco.org/issues/bug32 for example).

If you want to make an object available at namespace scope in a header
without order-of-initialization issues, you can use this technique

  namespace mine {

  template <class T>
  struct singleton
  {
     T& instance() { static T x; return x; }
  };

  namespace 
  {
    Whatever& x = singleton<Whatever>::instance();
  }

  }

HTH,
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.