-
Notifications
You must be signed in to change notification settings - Fork 230
Avoid static variables and functions in header files. #1405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks. I have no objections (apart from one question), it's just that there are much more functions that are marked as |
|
Yes, there are hundreds of such functions throughout BOOST :-) I'm an incrementalist in that I think that starting somewhere is better than waiting for a patch that fixes everything. So this is an increment :-) Would you like me to update the patch to use |
It depends per library. Boost.Geometry is at C++14 so the |
I see, thanks. It just seemed a bit a random selection. I think we can remove all of them in one go (maybe with a few exceptions) |
|
On 5/9/25 13:18, Barend Gehrels wrote:
Boost.Geometry is at C++14 so the |inline| should be removed for the variable.
But you can make it |constexpr|, please do. Hope that will be sufficient for
your use case.
No, it's the 'inline' that's important for me. To export the variable in a
C++20 module, I need it to have external linkage, which I can only get by
using 'inline'. Is there a BOOST macro that makes something inline if we use
C++17 or later, and expands to nothing for C++14 and earlier?
|
I see, thanks. Yes, there is, you can use
Alternatively there is also |
|
It's included by |
|
About my remark
I think you have most, or all, already in So after changing |
tinko92
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, if this needs inline I'd certainly be in favour of the constexpr inline macro to make the constexpr explicit, even when it is implied.
Is this change made redundant by DR2387 applied to C++14 and is there a compiler with sufficiently working module support to work for boost but no implementation of DR2387? If the answer is no to the first or yes to the second then I'm in favour of merging with use of the macro.
barendgehrels
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
I marked the two variables as |
DR2387 is unrelated. As for the compiler I'm using: Clang++-20 is sufficient for me to build modules for a project with 1000+ files and 1M+ lines of code. In fact, Clang-20 does not complain about the code I'm fixing here (with the exception of the two variables that need to be inline). It is GCC-15 that is more picky and complained about the places I'm pointing out here. GCC-15 has many other problems I am still working around. |
|
@bangerth it's all green. I'll merge it right now. Thanks again for the contribution. |
|
Thank you, too! I will likely have more patches of this kind to various BOOST libraries. |
I am working on converting a large scientific library (https://www.dealii.org) that builds on BOOST to C++20 modules, see dealii/dealii#18071. As part of this effort, I have to find ways to "wrap" BOOST into a module partition, something that will look like this file: https://github.com/bangerth/dealii/blob/cxx-modules/cxx20-modules/interface_module_partitions/boost.ccm
Wrapping works by "exporting" certain BOOST names via
usingdeclarations, but the targets of theseusingdeclarations can only be names that have "external linkage". This excludesstaticvariables and functions because they have "internal linkage". GCC 15 also requires that all names referenced by entities I want to export also have external linkage, and this turns up quite a large number ofstaticvariables and functions in BOOST header files.There are good reasons to avoid internal linkage variables and functions in header files beyond my desire to use C++20 modules. In particular, they should be avoided because they are replicated in each translation unit that
#includes a header file; if the object had external linkage, the linker would unify these copies into one, reducing code size.This patch addresses a number of places where this BOOST sub-library uses names with internal linkage in header files, by removing the
statickeyword and, where applicable, marking variables asinline. (Variables marked asconstorconstexprimplicitly have internal linkage unless marked withinline.)