Boost logo

Boost :

From: Felipe Magno de Almeida (felipe.m.almeida_at_[hidden])
Date: 2006-07-07 17:05:05


On 7/7/06, Dean Michael Berris <mikhailberis_at_[hidden]> wrote:
> On 7/6/06, Emil Dotchevski <emildotchevski_at_[hidden]> wrote:
> >
> > template <class T>
> > exception_info * get_exception_info( T & );
> >
> > It can be used in a catch( T & ) block, to check if the exception object has
> > a sub-object of class exception_info.
> >
>
> Why return a pointer? How about considering the use of a reference
> instead? Perhaps:

I prefer the pointer, that way one can define the variable inside the if

if(exception_info* p = get_exception_info(e))
{

}

>
> template < typename T >
> exception_info & get_exception_info ( T & );
>
> That way, the return can only be accessed using an exception_info
> reference. Consider this example (which is considerably cleaner to
> read, IMO) :
>
> try {
> throw failed<read_error>() << wrap_string<tag_file_name>("example.txt");
> } catch (read_error & e) {
> exception_info info = get_exception_info(e);
> if (info) {
> // deal with the info...
> }
>
> // deal with the exception...
> };

IMO, the pointer is cleaner.

>
> This also avoids the possibility of mis-use of pointers, because
> client code can still do a cast of the pointer to refer to something
> else -- which although it is hackish, is something you'd want to
> avoid. It also makes the following possible:

casts can be done with references just a-like.

>
> std::cout << get_string<tag_file_name>(get_exception_info(e)) << std::endl;
>
> Which is a little long for my taste, but readable nonetheless.
>
> 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

-- 
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