|
Boost : |
Subject: Re: [boost] [optional] How to define boost::none?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2014-11-19 11:04:34
On Wed, Nov 19, 2014 at 6:56 PM, Andrzej Krzemienski <akrzemi1_at_[hidden]> wrote:
> 2014-11-19 16:40 GMT+01:00 Sylvester-Bradley, Gareth <
> Gareth.Sylvester-Bradley_at_[hidden]>:
>
>> Hi Andrey,
>>
>> On Wed, Nov 19, 2014 at 3:27 PM, Andrey Semashev wrote:
>> > Since none has internal linkage, it is translation unit-specific.
>> > Technically, this can cause ODR violations in some cases. To avoid
>> > that you can declare it like this:
>> >
>> > struct none_t {};
>> >
>> > template< typename T >
>> > struct singleton
>> > {
>> > static const T instance;
>> > };
>> > template< typename T >
>> > const T singleton< T >::instance = T();
>> >
>>
>> // Do we need...
>> namespace
>> {
>>
>> > const none_t& none = singleton< none_t >::instance;
>>
>> }
>> // ?
>>
>> Thanks,
>> Gareth
>>
>
> With anonymous namespace the variable becomes TU-local (translation
> unit-local), which guarantees no ODR violation. But instead repeats the
> same declaration in every TU.
The reference becomes TU-local, the singleton instance has external
linkage, so it's shared between TUs. I'm not sure if the reference
declared this way actually adds anything to the binary size.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk