Boost logo

Boost :

From: John Torjo (john.groups_at_[hidden])
Date: 2007-10-03 06:00:36


Hi all,

I've watched the throw some_error<e1,e2,>(...) discussion:

> > Could you please explain? How does using Boost Exception require more
> > boilerplate code than the traditional approach?
>
> I mean the throw site:
>
> throw an_error(a,b,c)
>
> vs.
>
> throw an_error() << boost::error_info<a_tag>(a)
> << boost::error_info<b_tag>(b) << boost::error_info<c_tag>(c)

Why not have a class like this:

// t1, t2, t3 - tag classes
template<class t1, class t2, class t3...> struct tags {
  // not sure if "underlying_type" is the actual underlying type but you
catch my drift
  typedef typename t1::underlying_type p1;
  typedef typename t2::underlying_type p2;
 
  // hold the arguments
  tags(p1 a1, p2 a2, ...) : m_p1(p1), m_p2(p2) ... {}

  p1 m_a1;
  p2 m_a2;
  ....
};

template<class t1, class t2,...>
boost::exception & operator<<(boost::exception & e, const tags<...> & t) {
  e << t1(t.m_a1) << t2(t.m_a2) << ...;
  return e;
}

This way, you can have your cake and eat it too:

typedef tags<tag_file_name,tag_open_mode,tag_errno> file_err;
throw my_error() << file_err(name,mode,errno);

Best,
John

-- 
http://John.Torjo.com -- C++ expert
... call me only if you want things done right

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