With auto
I can create variables of a private type. For example, if for any reason, I create a public function returning an object of a private type, I could receive variables of that type with auto
:
class A
{
private:
struct B {};
public:
B& public_function_because_of_a_mistake();
};
A a;
auto& b = a.public_function_because_of_mistake();
// Perhaps, struct A::B is not properly design to support copying of elements.
// In the natural use of the class B (inside A), perhaps,
// there is no copy of objects of type B, so, the programmer didn't take
// the proper precautions.
decltype(b) b2 = b; // Unintended copy
// Perhaps the user wanted to call other function with same name, thinking 'b' was
// of another class.
b.something_unintended();
I know such a situation (so many unintented mistakes) is highly unlikely, but, anyway, isn't it a "security" or "design" hole? Because the main focus of C++ is to prevent unintented actions by means of a strict type system and access control rules, and auto
can skip them easily.
Aucun commentaire:
Enregistrer un commentaire