Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2007-06-14 20:29:59


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

> David Abrahams wrote:
>> on Thu Jun 14 2007, Eric Niebler <eric-AT-boost-consulting.com> wrote:
>>> I also seem to recall that there was still some question as to whether
>>> the use of the reference violated the ODR, if the reference is not
>>> optimized away. I don't think we ever got an answer to that question.
>>
>> I don't think there ever was any question about that. At least, I
>> never had a question about it. References don't have any identity
>> distinct from the object they reference.
>
>
> Funny you should say that, since you were the one who sent the "Are
> references subject to ODR?" question to the std reflector:
>
>> David Abrahams <dave_at_[hidden]>
>> 4/1/06
>> reply-to c++std-core_at_[hidden]
>> to undisclosed-recipients <>
>> date Apr 1, 2006 9:02 AM
>> subject Are references subject to ODR?
>> To: C++ core language mailing list
>> Message c++std-core-11390
>>
>> Is the following program legal?
>>
>> // a.cpp
>> int x;
>> int& y = x;
>>
>> // b.cpp
>> extern int x;
>> int& y = x;
>>
>> My impression is that it is not legal because references are not
>> explicitly given an exemption in 3.2/5
>>
>> If I am right, should it be so? It's hard to imagine how this could
>> be a problem.
>
>
> 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
   }

-- 
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