Re: This HAS to be UB...
On Oct 3, 1:18 am, blargg....@gishpuppy.com (blargg) wrote:
In article <2y9Fk.39817$hX5.39...@newsfe06.iad>, "Chris M. Thomasson"
<n...@spam.invalid> wrote:
Keep in mind that I am a C programmer; well, anyway here is the C++
program...
______________________________________________________________________
#include <cstdio>
#include <cstdlib>
#include <new>
struct custom_allocator {
static void* allocate(std::size_t size)
throw(std::bad_alloc()) {
***^^***
void* const mem = ::operator new(size);
std::printf("custom_allocator::allocate(%p, %lu)\n",
(void*)mem, (unsigned long)size);
return mem;
}
[...]
How did this even compile?
It's perfectly legal (or is it?), although certainly not what he
meant or wanted. The line in question is an exception
specification. The argument in parentheses is interpreted as a
type-id. In this case, the type is "function returning
std::bad_alloc and taking no arguments". Although I can't find
where the standard says so explicitly, I would expect the same
transformations to apply here as those which apply to return
values and function parameters: that a type function ... is
converted to a type pointer to function ...
As the standard doesn't say explicitly that this transformation
occurs in an exception specification (or at least, I couldn't
find) it, one can argue that it is illegal. Except that the
standard doesn't say anything about the types in this case
(except that the type can't be incomplete, nor can it be a
pointer or a reference to an incomplete type, except for
pointers to void), so if the transformation doesn't take place,
then the code is still legal, and the only exceptions allowed to
propagate are functions (which, of course, can't be thrown).
Regardless of the legality, I'm pretty sure that it wasn't what
he wanted. I'm also pretty sure that it has no impact on the
problem he described.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34