In the code below, I thought that, if I simply forget to return something, it'd simply return the default std::shared_ptr<int>
which should be an empty pointer, or a pointer equal/that points to nullptr
.
#include <stdio.h>
#include <memory>
#include <iostream>
std::shared_ptr<int> s() {
}
int main(void) {
auto a = s();
if (a) {
std::cout << "has something" << std::endl;
} else {
std::cout << "has nothing" << std::endl;
}
}
However running it gives me "illegal instruction", which I think it's undefined behaviour.
Why simply C++ does not enforce me to return something if it fails to assing a default value for the returned value? This is pretty dangerous.