Boost logo

Boost :

From: Felipe Magno de Almeida (felipe.m.almeida_at_[hidden])
Date: 2006-07-07 22:14:11


On 7/7/06, Dean Michael Berris <mikhailberis_at_[hidden]> wrote:
> On 7/8/06, Felipe Magno de Almeida <felipe.m.almeida_at_[hidden]> wrote:
> >
> > I prefer the pointer, that way one can define the variable inside the if
> >
> > if(exception_info* p = get_exception_info(e))
> > {
> >
> > }
> >
>
> I don't see the benefit in this since if exception_info is defined in
> the following sense:
>
> struct exception_info {
> exception_info() : is_valid(false) {};
> exception_info(const exception_info & other) : is_valid(other.is_valid) {};
> bool is_valid;
> ...
> operator bool () {
> // return true if it's a copy of a real
> // exception, false if it's uninitialized...
> return is_valid;
> }
> ...
> };

Is it? I thought an expcetion_info was always valid and as the
dynamic_cast works, it would return a null pointer in case there wasnt
any object.
Besides, how are you always returning a reference? How to take care of
exception_info's lifetime?
You're not easing client's lifes. If you want to receive a reference,
you would have to return a temporary and hold it by that reference (it
is possible because the temporary's life would be prolonged). But that
doesnt buy anything since all you would be doing is making the
learning curve steeper for the library all to prohibit pointer
aritmetic. Which, IMO, is a little hard to see anybody with some sense
doing in this case.

> Then something like this will be possible:
>
> if (get_exception_info(e)) {
> std::cout << get_string<tag_file_name>(get_exception_info(e)) << std::endl;
> }

You would have to pay the dynamic_cast for each call. Besides,
repeating get_exception_info, IMO, only degrades readability

>
> >
> > IMO, the pointer is cleaner.
> >
>
> Since a pointer is basically a primitive type which you can manipulate
> like an integral type, it opens up a can of worms you don't want to
> deal with -- and will only make the code a lot easier to get wrong.
> Since you can manipulate pointers like int's, the following operations
> will be valid:
>
> exception_info * x = get_exception_info(e);
> x++;
> ++x;
>
> But if you're using references, you give a cleaner and more definite
> handle to your data:

The idea here is use the pointer to be used in a conditional. Just
like when someone uses dynamic_casts.

if(A *p = dynamic_cast<A*>(obj))
{

}
else if(B* p = dynamic_cast<B*>(obj))
{

}

and so on...

>
> exception_info & info (get_exception_info(e));
> info ++; // won't work, unless operator++ is defined for exception_info

Which is too much trouble only for prohibiting pointer aritmetics.

>
> HTH
>
> --
> Dean Michael C. Berris
> C/C++ Software Architect
> Orange and Bronze Software Labs
> http://3w-agility.blogspot.com/
> http://cplusplus-soup.blogspot.com/
> Mobile: +639287291459
> Email: dean [at] orangeandbronze [dot] com

best regards,

-- 
Felipe Magno de Almeida

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk