-
Notifications
You must be signed in to change notification settings - Fork 5
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
insert(value_type const& x).second is always true #9
Comments
According to https://en.cppreference.com/w/cpp/container/set/insert:
|
I understand that interpretation, but it is quite common to have code considering the return value of insert to reflect whether a previous call to be the negation of whether a previous call to contains would have returned true or not. Whether an insert is successful when the unconditional bitwise-OR ends up not affecting anything in the representation is then a matter for some debate. The return value is not used as an error reporting mechanism for e.g. failed memory allocations, those will be thrown instead. I don't have access to the standard right now, but I think this behavior would violate the actual wording of the standard. From a more pragmatic standpoint, it is a possible source of bugs when trying to use bit_set as a dropin replacement for set. I understand your design choice, though and still appreciate your library. Except for this detail, it suited my needs quite well. I was mostly surprised when I realized after a while why my code produced incorrect results. The hassle mostly arises from the fact that I tried to maintain one codepath that could be compiled with bit_set if present and set otherwise, so doing the explicit check for the element being present beforehand would incur a cost when an ordinary set is used. |
Would it help if I added an |
That would make sense to me, making the identically named one behaving in an identical way to a conventional set, but keeping ready access to a shortcut version doing a plain insert. |
Finally got around looking at this. You are right: the Standard mandates returning the presence prior to insertion. Also |
I fixed |
Should be fixed now, If not, I'll re-open. |
A proper set<> will return true only if the element did not already exist. The current implementation always returns true.
The fix to check whether the bit is already set seems trivial, but I suppose that might incur some performance penalty unless the compiler will be able to conclude that the return value is not used (which is, arguably, the most frequent case).
The text was updated successfully, but these errors were encountered: