The following code compiles without any errors/warnings with gcc 6.3 (http://ift.tt/2iOknSQ), but it contains a dangerous undefined behavior due to invalid memory access marked below. The root cause is the implicit conversion performed in emplace_back. Can anyone suggest a good way or best practices to avoid such bugs in code?
#include <iostream>
#include <vector>
struct Foo
{
explicit Foo(const int& i) : i{i} {}
void foo() const { std::cout << i; } // invalid memory access, as i is an invalid ref!!
const int& i;
};
void bar(const double& d) {
std::vector<Foo> fv;
fv.emplace_back(d);
}
Aucun commentaire:
Enregistrer un commentaire