Prev: Temporaries passed to constructor yield invalid member references
Next: Names or traits for std::random param_type access methods?
From: Razvan Cojocaru on 1 Jun 2010 22:52 > The difference is that you can pass a temporary as a const-ref without > seeing it, while you cannot pass a temporary as a pointer without some > syntactic overhead. (This statement does *only* apply to the immediate > call - it does not apply to general lifetime management.) But lifetime management is a very important issue, and as another poster has pointed out, the pointer solution is very easy to mess up by passing NULL or the address of a temporary object (either like his example, &TYPE(), or by passing &function_returning_a_temporary()). Also, you can't talk about using pointers (and even to some extent references) without having to consider the copy semantics of the class, which complicates matters further. And last but not least, you've recommended a const pointer, which in my book is "type* const p", however the original poster has used a pointer to const data in his example implementation ("const type* p") - so we get into easy to misunderstand terminology as well. Too easy to make a mistake in the future, even in good faith. So while indeed he can fool the compiler into giving in, I think there's a strong posibility that the solution is too smart for it's own good. Just my 2 cents, -- Razvan Cojocaru KeyID: 0x04CA34DE [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |