Boost logo

Boost :

Subject: Re: [boost] [system] Header-only Boost.System by default ?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2017-10-10 07:32:23


On 10/10/17 10:18, Damien Buhl via Boost wrote:
> On 09/10/2017 19:34, Niall Douglas via Boost wrote:
>>> Is there any reason why Boost system couldn't be set to be header only
>>> by default ?
> [...]
>> The only correct and safe way to use error categories is from a shared
>> library. In header only mode, multiple instances may appear and thus no
>> longer be proper singletons. Stuff breaks in this situation, badly.
>
> If I implement an error category of my own like this :
>
> ```cpp
>
>   inline const basic_error_category& get_basic_error_category() {
>     static basic_error_category category{};
>     return category;
>   }
>
>   static const boost::system::error_category& basic_error_category =
> get_basic_error_category();
>
> ```
>
> Is there really no guarantee that category always get the same address
> across TUs ?

It will have the same address as long as get_basic_error_category() is
called within the same module (dll, so, exe, etc.) If this function is
compiled in different modules then there will be multiple instances of
the category with different addresses.

On Linux and probably other Unix-like systems this can be solved by
exporting the category instance, which is the default. I don't think
this can be done on Windows - you'd have to export
get_basic_error_category for that instead and it will make this code
less friendly to header-only libraries.

> It has been my understanding of extern inline linkage that it actually
> is so.
> The linker keeps only one copy in the final program. In C++17 we could
> even use the static inline specifier for variables.

I don't think inline variables will solve this problem.


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