Shared_ptr and constness
Hi, Just out of curiosity. I am increasingly using the smart_ptr and more specifically the shared_ptr but I came to realize that actually it rather violently breaches the constness rules. The example below is one of the many things you can do with a shared pointer that is forbidden with a naked pointer. As the shared_ptr penetrates even further in my code I come to realize that what I am gaining on the comfort side I loose on the safety. Clearly, that's my call but I was curious to know if it is a known fact that it is impossible to enforce constness rules. Cyril Godart Quantitative Analyst BNP Paribas. struct A { int bar; }; typedef boost::shared_ptr<A> A_ptr_t; void foo(const A_ptr_t& a) { a->bar++;//semantically this should not be possible. } int main(int argc, char* argv[]) { A_ptr_t a(new A); a->bar = 1; foo(a); return 0; } This message and any attachments (the "message") is intended solely for the addressees and is confidential. If you receive this message in error, please delete it and immediately notify the sender. Any use not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval. The internet can not guarantee the integrity of this message. BNP PARIBAS (and its subsidiaries) shall (will) not therefore be liable for the message if modified. --------------------------------------------- Ce message et toutes les pieces jointes (ci-apres le "message") sont etablis a l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce message par erreur, merci de le detruire et d'en avertir immediatement l'expediteur. Toute utilisation de ce message non conforme a sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. L'internet ne permettant pas d'assurer l'integrite de ce message, BNP PARIBAS (et ses filiales) decline(nt) toute responsabilite au titre de ce message, dans l'hypothese ou il aurait ete modifie.
Hi,
Just out of curiosity. I am increasingly using the smart_ptr and more specifically the shared_ptr but I came to realize that actually it rather violently breaches the constness rules. The example below is one of the many things you can do with a shared pointer that is forbidden with a naked pointer. As the shared_ptr penetrates even further in my code I come to realize that what I am gaining on the comfort side I loose on the safety. Clearly, that's my call but I was curious to know if it is a known fact that it is impossible to enforce constness rules.
Cyril Godart Quantitative Analyst BNP Paribas.
struct A { int bar; }; typedef boost::shared_ptr<A> A_ptr_t; void foo(const A_ptr_t& a) { a->bar++;//semantically this should not be possible. } int main(int argc, char* argv[]) { A_ptr_t a(new A); a->bar = 1; foo(a);
return 0; }
In this situation, I believe you need to use a shared_ptr<const A> to get the behavior you want. Think of the difference between A const* (or const A* if you prefer) and A* const. The latter is analagous to your use of shared_ptr in the example above (const pointer to non-const object). Scott McCaskill
participants (2)
-
cyril.godartï¼ bnpparibas.com
-
Scott McCaskill