|
Boost Users : |
Subject: Re: [Boost-users] [exception] array tags
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-10-25 02:56:33
On Sat, Oct 24, 2009 at 8:09 PM, Diederick C. Niehorster
<dcnieho_at_[hidden]> wrote:
> Hi,
>
> I have just discovered boost::exception and am very happy I did. One
> thing i would like to do, but haven't yet figure out:
>
> I have a matrix class for which I want to throw exceptions when
> out_of_range requests are made. One of the extra tags I want to attach
> is the dimensions of the matrix and it would be neat if i can put
> those in an array. Defining the tag is no problem, getting the info in
> there is:
>
> defining:
> typedef boost::error_info<struct tag_std_range_size,size_t[2]> std_range_size;
>
> I have tried:
> << std_range_size({m_nRows, m_nCols});
> size_t a[2] = {m_nRows, m_nCols};
> << std_range_size(a);
> and desperately even << std_range_size() = {m_nRows, m_nCols}; which
> doesn't make much sense
Option 1, you can use boost::tuple with two separate tags:
#include <boost/exception/info_tuple.hpp>
typedef boost::error_info<struct std_range_rows_,size_t> std_range_rows;
typedef boost::error_info<struct std_range_cols_,size_t> std_range_cols;
typedef boost::tuple<std_range_rows,std_range_cols> std_range_size;
With this in place you can throw whatever() << std_range_size(m_nRows,m_nCols);
The above still adds two individual error infos: at the catch site
you'd still need separate get_error_info calls to get the rows and the
cols.
Option 2, you can put rows and cols in a struct and make that your
error_info type:
struct rc
{
size_t rows, cols;
rc( size_t r, size_t c ): rows(r), cols(c) { }
};
typedef boost::error_info<struct std_range_size_,rc> std_range_size;
and then throw whatever() << std_range_size(m_nRows,m_nCols);
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net