-
Notifications
You must be signed in to change notification settings - Fork 56
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
boost::units::quantity should be trivial #58
Comments
(Disclaimer: I am not a developer of Boost.Units, I am just a user, and I think there are strong arguments for this). I agree with this. For a while, I mistakenly thought that it did make sense for quantities to have a (non-trivial) zero initialization because zero values are, well, special for quantities/measurements. Many types are unfortunately stuck on being non-trivial when they shouldn't be, for example, std::pair of trivials or even std::complex. I don't know why this problem is so prevalent in old (even standard) libraries; I think the problem is that in C++98, it was not very easy to make types trivial (no This an analogous request I made to have another complex type be trivial: NVIDIA/thrust#1079 And I am afraid what you bring up is exactly the same problem for std::quantity, which I also consider a numeric type. As much as I would love std::quantity to be trivial (especially trivially constructor), I hate to be the bearer of bad news: the problem looking forward is that if some people already relied on zero initialization, this will be a breaking change. Finally, as another datapoint, note that |
There could be ifdefs for such things. There are no necessities. Just gut feelings (hope they ate well). |
It indeed should be trivial-without it generic code using atomics does not work:
|
@jhunold , @swatanabe : Any hope for getting this patchfile accepted? |
If changing behaviour is not an option at all, I'd suggest introducing new namespace for such fixes. |
@hlewin : What behavior is changed? |
@NAThompson, as I said (in my convoluted comment), the current behavior initializes the value to zero. A proper trivial type doesn't initialize to zero; it "leaves" the value uninitialized. If someone's code relied on that default initialization producing a zero value, it would be broken by making the type trivial. It is unfortunate that quantity was not trivial from the start. I had a similar dilemma concerning changing behavior. For that, I have boost::units::implicit::quantity, which, in practice, I use as
Analogously, I think what @hlewin is saying is that you can have: |
The problem being that |
Yes, I agree. I was referring to the fact of using a new class only. That is why trivializing a class after the fact is so difficult. |
It is not: Declare the old behaviour deprecated and provide a define like |
@hlewin I couldn't agree more that quantity should be trivial if T is trivial. I am just saying that it would be a breaking change. Moreover, I would agree with the breaking change if it were in my hands. |
Yeah, point is: Writing a patch to whatever spec is not really a problem. But I am not gonna waste that time if the general sentiment is to not accept it one way or the other. If there are arguments like "someone might have taken the sha256 checksum of that header so changing it might break a build" I am just outta here. I already have this patched in company local versions. |
@hlewin , well, let's see what the developers say, if they say something. (I am not a developer or maintainer of Boost.Units). I have my own |
My own preference would be to make a breaking change, because well, std::atomic doesn't work with these types due to the triviality requirements, and therefore much of my multithreaded code simply doesn't compile with these types. |
Discussing the technicalities of a fix would only make sense if there would be consensus about that this is a problem with someone who can make such decisions. I don't see that. Zero-initialization of numerical values was a javaesque design decision back then and did not age well on the c++ side either. So... I don't hold my breath. |
AMDG
On 2/12/24 00:16, Heiko Lewin wrote:
Discussing the technicalities of a fix would only make sense if there would consensus about that this is a problem with someone who can make such decisions. I don't see that. Zero-initialization of numerical values was a javaesque design decision back then and did not age well on the c++ side either. So... I don't hold my breath.
Since =default didn't exist back in 2006, and quantity has other
constructors, the choice was between having no default constructor,
having the default constructor zero-initialize, or allowing
value-initialization to leave the object uninitialized.
Zero-initialization was the least harmful option at the time.
Making it trivial is obviously the right choice if we were
implementing it from scratch today, but I'm very hesitant to
make such a change, as it would silently cause previously
correct code have undefined behavior.
With that being said, the copy constructor, destructor,
and copy-assignment operators definitely can and should
be defaulted.
In Christ,
Steven Watanabe
|
Those concerns can be addressed though, ie the existing behaviour could be preserved for legacy code with a maintenance cost close to zero. First I already mentioned the possibility of utilizing defines and/or deprecation declarations. So from the top of my head I'd propose the following:
2a) At the very least it should now possible for a user of the lib to provide specialisations of Depending on the approach taken one could discuss how and to what extend the library and dependent source-code should be steered towards "the right way". |
My impression is that serious users slowly discover all the problems with this library, then either give up or fork the repo-which seems sad given how little effort it would take to fix these small problems. I'd be very curious to see how many users actually exist and how many give up due to inability to compile large codebases on this type. IMO the deprecation warning is the way to go and then make a breaking change in 2-3 releases. |
I think
boost::units::quantity<>
should be as trivial as possible.Right now they are not ( std::is_trivial_v is false ) because of more-or-less superfluous implementations of constructors and assignment operators. The internally asserted layout compatibilities have no meaning for users of the class.
I do not really have a strong argument for making them trivial but have a strong gut feeling they should be (for users at least).
The text was updated successfully, but these errors were encountered: