Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-06-14 20:49:22


David Abrahams wrote:
> on Thu Jun 14 2007, Eric Niebler <eric-AT-boost-consulting.com> wrote:
>> The result of ensuing discussion was that a reference is a pointer, and
>> so they *do* have identity.
>
> That's a different question than I thought you were referring to. 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?

-- 
Eric Niebler
Boost Consulting
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