#include <iostream>
struct A {
int x;
};
void foo(A a) {
std::cout << a.x << std::endl;
}
int main() {
A a;
foo(a); // -7159156; a was default-initialized
foo(A()); // 0; a was value-initialized
}
Is it possible to pass an rvalue of type A
to foo()
without value-initializing it? Do we have to use either value-initialization or an lvalue?
What's the point of avoiding value-initialization when it "costs" no more than ten nanoseconds, you may ask. How about a situation like this: we are hunting for a bug in an legacy app caused by an uninitialized memory access with valgrind, and zero is NOT considered as a valid value for the app. Value initialization will prevent valgrind to spot the location of uninitialized memory access.
You may say printing an uninitialized value is an UB but my "real" use case is not limited to printing. My question should remain valid without it.