Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2007-06-14 23:22:54


on Thu Jun 14 2007, Eric Niebler <eric-AT-boost-consulting.com> wrote:

> David Abrahams wrote:
>
>> I would put the reference itself in an unnamed namespace, and I'm
>> pretty sure that handles it. As long as the reference refers to
>> something not in an unnamed namespace:
>>
>> template<typename T> struct static_const
>> {
>> static T const value;
>> };
>>
>> template<typename T>
>> T const static_const<T>::value = {};
>>
>> struct placeholder {};
>>
>> namespace // <<============== HERE
>> {
>> placeholder const &_1 = static_const<placeholder>::value;
>> }
>>
>> template<class T> void foo(T const &)
>> {
>> // use _1 here. OK, _1 refers to the same object
>> // in all translation units. It doesn't matter that the
>> // reference is has a different identity; the object is
>> // the same: static_const<placeholder>::value
>> }
>
>
> Does that really change anything? Imagine replacing the reference
> with a const pointer, and the potential for an ODR violation becomes
> more obvious:
>
> template<typename T> struct static_const
> {
> static T const value;
> };
>
> template<typename T>
> T const static_const<T>::value = {};
>
> struct placeholder {};
>
> namespace
> {
> placeholder const *const _1 = &static_const<placeholder>::value;
> }
>
> template<class T> void foo(T const &)
> {
> // use *_1 here. Even though *_1 refers to the same
> // object, we're indirecting through a different pointer
> // in different TUs. ODR violation?
> }
>
> Since you're indirecting through a different pointer, the code
> generated for foo() would be different in different translation
> units. Isn't that a violation of the ODR?

The difference is that the references themselves do not have an
address. The address is the only feature of those two const pointers
that makes them observably distinct. The core gods, of course, may
still disagree :-)

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk