Boost logo

Boost :

Subject: Re: [boost] [algorithm][hex] function hex_char_to_int in unnamed namespace in header file
From: Felipe Magno de Almeida (felipe.m.almeida_at_[hidden])
Date: 2012-12-03 15:09:33


On Mon, Dec 3, 2012 at 5:58 PM, Marshall Clow <mclow.lists_at_[hidden]> wrote:
> On Dec 3, 2012, at 9:43 AM, Felipe Magno de Almeida <felipe.m.almeida_at_[hidden]> wrote:

[snip]

>> If any entity defined in a unnamed namespace is used in
>> an external linkage definition, such as a
>> inline function or a template definition and this definition is used
>> in multiple TUs it causes ODR violations, because
>> it is defined differently (uses different entities) in each TU.
>
> And No. Because the call to it (in each TU) will call the entity in the specific unnamed namespace that was part of that translation unit.

Yes. The problem is not the call. Is that the definition of the inline
function and/or template are different from each other. Because they
refer to different functions in each TU. And that is a ODR violation.

Let me try to illustrate:

-- foo.h
namespace {
void foo() {}
}

-- bar.h
#include "foo.h"
template <typename T>
void bar(T object)
{
  foo();
}

-- file1.cpp
#include "bar.h"

bar(5);

-- file2.cpp
#include "bar.h"

bar(5);

--
The template bar is defined differently for file1.cpp and file2.cpp because each
identifier do not represent the same entity. Which is a ODR violation, since
their definitions must equal to each other. The same would apply just
as well for
an inline function.
> My mental model for unnamed namespaces is "a namespace with a unique compiler-generated name"
Yes, the same here. The problem is when a function,outside a unnamed
namespace, uses something
defined in a unnamed namespace, its definition can't be equal across
multiple TUs anymore,
because the namespace has a different "unique compiler-generated name".
> -- Marshall
>
> Marshall Clow     Idio Software   <mailto:mclow.lists_at_[hidden]>
>
> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
>         -- Yu Suzuki
Regards,
-- 
Felipe Magno de Almeida

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