Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2008-07-14 07:54:51


on Sun Jul 13 2008, Maxim Yanchenko <maximyanchenko-AT-yandex.ru> wrote:

> David Abrahams <dave <at> boostpro.com> writes:
>
>> Isn't this a bit of an invitation to ODR violations?
>>
>> // TU1
>> #include "MaximIO.hpp"
>> #include "boost/range/iterator_range.hpp"
>> #include "LibraryThatPrints.hpp"
>>
>> print(some_iterator_range);
>>
>> // TU2
>> #include "boost/range/iterator_range_io.hpp"
>> #include "boost/range/iterator_range.hpp"
>> #include "LibraryThatPrints.hpp"
>>
>> print(some_iterator_range);
>>
>> If the print template calls different IO functions in these two
>> translation units, that's an ODR violation.
>
> Hi David, that's a good question, and it also addresses boost.tuple and other
> libraries with detachable io (and detachable anything).

Only when there's going to be another overload that matches. As long as
there's only one definition of tuple streaming, we're OK. My point
isn't that separating the IO is dangerous, although it is a little. My
point is that the specific thing you are trying to do is dangerous.

> In my case, there is no direct ODR violation, as there is actually no print
> function, but an operator<< living in a separate namespace instead.
> So when you include "MaximIO.hpp" and use "using MaximIO::operator<<" all your
> calls to operator<< will be resolved to MaximIO::operator<<,

That won't work unless the using declaration is visible from all the
templates that stream these iterator ranges (e.g. std::for_each).

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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