Boost logo

Boost :

Subject: Re: [boost] [config] request for BOOST_NO_FWD_STD_DECLARATION
From: Christopher Jefferson (chris_at_[hidden])
Date: 2011-02-16 10:30:12


On 16 Feb 2011, at 15:18, Loïc Joly wrote:

> Le 16/02/2011 16:06, Christopher Jefferson a écrit :
>>
>> Even if subtle readings of the standard turned out to forbid it, using inline namespaces to implement std:: is here to stay, and I expect it to get more popular as time goes by.
>>
>
> My question is more the following: Should'nt forward declaration work even if the element turns out to be inside an inline namespace ?

It can't do, practically (random details follow)

The main purposes of inline namespaces is that the name of the inline namespace gets mangled into the object name, so

namespace std { inline namespace version1 { struct pair; } }

void my_function(std::pair);

The compiler creates a "mangled" string, which represents uniquely this function, including it's name and all the types it accepts. This is used by the linker to connect together calls to functions, and the function definitions. It is something like (but not, as this is a simple example): _Fmy_function__Pstd__version1_pair.

Now when we do a new version we define:

namespace std { inline namespace version2 { struct pair; } } then my_function gets mangled to _Fmy_function__Pstd__version2_pair.

This has lots of advantages. Code which uses both my_function on a version1 pair and version 2 pair works, and calls the right my_function. If you have a library which has a my_function compiled for a version1 std::pair, and try to call it from code which uses version 2, you will get a link-time failure.

If the "mangled name" of both pairs were the same, linking would work but if the version1 pair and version2 pair did not have the same binary layout, horrible things would happen.

This means that if we just see: namespace std { struct pair; }, we can't know which inline namespace to put it in. The only way to make this work would be to not mangle the name of the inline namespace into the object, which would defeat the purpose.

Chris


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