Topic: Library solution possible (was: C++0x: Proposal for an additonal exception handling construct)


Author: semzx@newmail.ru (Alexander E. Patrakov)
Date: Sat, 5 Oct 2002 12:13:35 +0000 (UTC)
Raw View
[ To moderators: please rename this message if you can invent a better
subject line ]
Alf P. Steinbach wrote in the original thread:

> To avoid this thread degenerating into a debate about
> new keywords (i.e., how difficult the keyword problem
> is to solve), I suggest
>
>  * Please start a new thread if you feel like tackling
>    the topic of how to (not) introduce new keywords.

So, I've done it.

I think that in the case when "3.3 Obtaining exception information" is
not used, a library solution exists, opposed to the language solution
proposed by Alf P. Steinbach. No new keywords are introduced.

I have not tried to implement it, though.

The idea:

Fill a vector with boost::function<A>'s, where A is the appropriate
return type of the multiple-attempt function. The elements of this
vector will represent individual attempts. If the actual functions
take arguments, we can make use of boost::bind.

Let's consider the following (not tested, may contain errors):

template<class A>
  A make_attempts(std::vector<boost::function<A> > attempts)
{
  if (attempts.empty())
    throw NoAttemptsSpecified();

  std::vector<boost::function<A> >::iterator it = attempts.begin();
  // The last attempt should be treated differently, so we have "- 1"
  std::vector<boost::function<A> >::iterator end = attempts.end() - 1;

  while (it != end)
  {
    try
    {
      return (*it)();
    }
    catch(...)
    {
      ++it;
    }
  }

  // All attempts failed except the last one - let's try it
  return (*it)();
}

So, in order to attempt multiple strategies, one just has to call
make_attempts(his_vector_of_attempts).

Sorry if this post contains mistakes. Please correct me.

The following questions remain:

1) Is it possible to implement "3.3 Obtaining exception information"
by means of a library?
2) I suspect that my code makes some assumptions on A, but I cannot
find them. Do they really exist? What are they? Is it possible to
avoid them?

--
Alexander E. Patrakov

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: alf_p_steinbach@yahoo.no.invalid (Alf P. Steinbach)
Date: Mon, 7 Oct 2002 17:29:11 +0000 (UTC)
Raw View
On Sat, 5 Oct 2002 12:13:35 +0000 (UTC), semzx@newmail.ru (Alexander E. Patrakov)
wrote:

>[ To moderators: please rename this message if you can invent a better
>subject line ]
>Alf P. Steinbach wrote in the original thread:
>
>> To avoid this thread degenerating into a debate about
>> new keywords (i.e., how difficult the keyword problem
>> is to solve), I suggest
>>
>>  * Please start a new thread if you feel like tackling
>>    the topic of how to (not) introduce new keywords.
>
>So, I've done it.
>
>I think that in the case when "3.3 Obtaining exception information" is
>not used, a library solution exists, opposed to the language solution
>proposed by Alf P. Steinbach. No new keywords are introduced.

Dave Harris (I think) described a similar scheme in the
[comp.lang.c++.moderated] debate about an earlier idea.  If I remember
correctly he called that an implementation of the "template pattern".

Very elegant but, ironically, difficult to templatize.

One problem with not supporting exception information is that that
also excludes general support for catching only a subset of
exceptions.

Another problem, I don't know how serious, is that an attempt cannot
then rethrow the exception from the previous attempt.

A third, and IMHO opinion the most serious, problem is that with
such a solution it is the programmer's responsibility to use it
correctly and to adhere to the implicit restrictions, with runtime
errors from incorrect usage possible: this programmer responsibility
is exactly what the throw-and-succeed construct is meant to avoid.



>I have not tried to implement it, though.
>
>The idea:
>
>Fill a vector with boost::function<A>'s, where A is the appropriate
>return type of the multiple-attempt function. The elements of this
>vector will represent individual attempts. If the actual functions
>take arguments, we can make use of boost::bind.
>
>Let's consider the following (not tested, may contain errors):
>
>template<class A>
>  A make_attempts(std::vector<boost::function<A> > attempts)
>{
>  if (attempts.empty())
>    throw NoAttemptsSpecified();
>
>  std::vector<boost::function<A> >::iterator it = attempts.begin();
>  // The last attempt should be treated differently, so we have "- 1"
>  std::vector<boost::function<A> >::iterator end = attempts.end() - 1;
>
>  while (it != end)
>  {
>    try
>    {
>      return (*it)();
>    }
>    catch(...)
>    {
>      ++it;
>    }
>  }
>
>  // All attempts failed except the last one - let's try it
>  return (*it)();
>}
>
>So, in order to attempt multiple strategies, one just has to call
>make_attempts(his_vector_of_attempts).
>
>Sorry if this post contains mistakes. Please correct me.
>
>The following questions remain:
>
>1) Is it possible to implement "3.3 Obtaining exception information"
>by means of a library?

I suspect not, but claims about impossibility have a tendency to
be proven wrong unless they're backed up by hard proof.  So far
nobody have suggested any mechanism, however.  One might interpret
that as nobody have been able to think of any mechanism.



>2) I suspect that my code makes some assumptions on A, but I cannot
>find them. Do they really exist? What are they? Is it possible to
>avoid them?

I'm not familiar with boost::function, but shouldn't there be an
object involved somewhere (to call member functions on)?

Cheers,

- Alf

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]