-
Notifications
You must be signed in to change notification settings - Fork 43
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
Value initialization not working #134
Comments
Yes. this is the intent. The fact demonstrated in your example and a close reading of https://en.cppreference.com/w/cpp/language/default_initialization#Notes convince me that my original understanding of initialization of int and other primitive variables was wrong. I had assumed that if initialization wasn't specified, it wasn't done. Turns out that it depends on the circumstances. If done, the value is zero. FWIW - another case where C++ has been gratuitously complicated - thus making it much harder to write a correct program. Primitive types such as int are initialized by default while non-primitives are not. Since safe is not a primitive type, it isn't initialized by default. I'll have to look into this. I'll leave this issue open as a reminder. Note that this subject is touched upon by another issue here. issue #90 In the interests of demonstrating program correctness, I would recommend explicitly initializing all variables. Note that in order to properly initialize safe and related types, you'll have to become familiar with safe_literal ... etc. |
Hello Robert, thank you for your reflection. I agree with you that c++ is extremely complicated in the area of initialization. It is definitely also beyond my understanding in its entirety. Reading your cited article, I also realized that my terminology was not fully correct. Here is my current understanding: Default initialization:This is when no "initializer" is provided, (default initialization). int x;
safe<int> x; This leads to "indeterminate values" for "non-class variables". So Here the library has the same behavior as using Value-initializationThis is when the initializer consisting of an empty pair of parentheses or braces (value initialization.). int x {};
int x = int ();
int x = int {}; Or when used in class members or constructors. class Foo {
int x {};
};
class Bar {
int x;
Test() : x() {}
}; In this case non-class variables are zero-initialized (zero initialization). Here the behavior of Possible ImplementationsClearly it would be best if I looked into this in the last 2 days intensely. However I don't see a straight-forward implementation. I made a list of possible ideas and the tradeoff. Please see my conclusion at the end. Option 1 - never initialize
|
Now thinking a bit more and reading #90, I think we should at least change the As currently those are ignore and using Perfectly valid C++ code: auto get_value_or_zero(std::optional<int> value) -> int {
if (value) {
return *value;
}
return {};
} Here using Int = boost::safe_numerics::safe<int>;
auto get_value_or_zero_UB(std::optional<Int> value) -> Int {
if (value) {
return *value;
}
return {}; // UB
} Not only that, but this code produces no compile time warnings even with |
Holly smokes. A huge effort on your part !!! Welcome to the world of Boost C++ library development. I'll chew on this a couple of days. Given the intensive and extensive efforts you've made, I'm very interested in getting these considerations incorporated into the the library. Looks like we'll have to investigate they issue: Is the a library for guaranteeing program correctness or is the goal to detect C++ errors. Anyway - thanks again and I'll try to looking into this soon. |
I am not sure if technically a bug, but from the documentation and talks what I understood was that in my code I can basically replace
int
withsafe<int>
and it should compile and give the same result with checks.I get into problems related to default initialization.
1. Default Initialization
See on godbolt.
2. Aggregate Initialization
This is obviously related, and I want to mention it, as this is how it came to my attention.
With
safe<>
See on godbolt
The text was updated successfully, but these errors were encountered: