|
|
|
|
|
by spacechild1
4 days ago
|
|
The example is pretty contrived because in practice you'd want to zero-initialize the members in the default constructor anyway. struct span {
const char *p = nullptr;
size_t len = 0;
span() = default; // or span() {}
span(const char *s, size_t n) : p(s), len(n) {}
};
In that case, there is no difference between span s;
and span s{};
There are only few use cases where you want a default constructor that does not initialize its members. One example are wrappers around primitive types where you want to closely preserve the original semantics. Whether that's a good idea in the first place really depends on the use case.For example, std::atomic used to default-initialize the underlying value, which led to many bugs in user code. Since C++20 std::atomic uses value-initialization. See https://cplusplus.github.io/LWG/issue2334. On the one hand, std::atomic is now safer to use, on the other hand, it is not an implicit-lifetime-type anymore, which means you can't put it in shared memory without UB... Another example is glm::vec, which has actually hit me a few times in my own code. At least they recognized the issue and allowed to override the default behavior with the GLM_FORCE_CTOR_INIT define. |
|