Article View: comp.std.c++
Article #1636Exceptions specifications for function templates
From: Harald M. Mueller
Date: Mon, 07 Aug 1995 07:54
Date: Mon, 07 Aug 1995 07:54
77 lines
3221 bytes
3221 bytes
I did no find an answer/hint/idea for this in the C++ standard:
What should the exception specification be for
template <class T>
void swap(T& a, T& b) throw(???)
{ T tmp = a; a = b; b = tmp;
}
- we do not know the exceptions that might be thrown by
the copy constructor or assignment operator of T!
As far as I can see, there are three ways out of this problem:
(a) Require that all called functions adhere to the
exception specification supplied, e.g.,
template <class T>
void swap(T& a, T& b) throw(bad_alloc)
{ T tmp = a; a = b; b = tmp;
}
Here swap must only be instantiated with types
where the copy constructor as well as the assignment
operator promise to throw nothing but bad_alloc
exceptions.
This, of course, makes it impossible to use the
template for types supplied by third parties who do
not know about this restriction.
(b) Catch unspecified exceptions possibly thrown inside the
calling function and throw an exception allowed by
the supplied exception specification:
class Xswap {}; // exception meaning "something
// went wrong in swap"
template <class T>
void swap(T& a, T& b) throw(Xswap)
{ try {
T tmp = a; a = b; b = tmp;
} catch(...) {
throw Xswap();
}
}
This, in effect, destroys all the information which
might be contained in a thrown exception, and
therefore reduces the information transported by an
exception to the single bit "something went wrong".
(c) Do not use exception specifications if unknown exceptions
might be thrown:
template <class T>
void swap(T& a, T& b)
{ T tmp = a; a = b; b = tmp;
}
The disadvantage of this is, of course, that no
automatic run-time check of the thrown exceptions is
possible.
Although I do it only reluctantly, I opt for version (c) because
the restrictions brought about by (a) and (b) are too heavy, in
my opinion - or did I overlook something???
Big Q: How do the standardization people intend to write the
exceptions specifications for all the function templates in
the standard?
HMMueller
== --------------------------------------------------------------
== Dr. Harald M. Mueller mueller@garwein.hai.siemens.co.at
== Siemens AG Austria Siemens AG Austria
== PSE EZE TNA1 Erdberger Laende 26
== fax: +43-1-71711-5425 A-1030 Vienna/Austria
== --------------------------------------------------------------
Technical Details
Message-ID:
<404gsg$st@siemens.co.at>Path:
arc1-ca.pugleaf.net!siemens.co.at!usenetReferences:
<3vj2m0$8it@engnews2.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!siemens.co.at!usenet From: mueller@garwein.hai.siemens.co.at (Harald M. Mueller) Newsgroups: comp.std.c++ Subject: Exceptions specifications for function templates Date: 7 Aug 1995 07:54:56 GMT Organization: Siemens Austria PSE EZE TNA1 Message-ID: <404gsg$st@siemens.co.at> References: <3vj2m0$8it@engnews2.Eng.Sun.COM> Reply-To: mueller@garwein.hai.siemens.co.at NNTP-Posting-Host: garwein.hai.siemens-austria