Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 2000-07-21 13:57:32


Marco Manfredini wrote:

> Just for curiosity
>
> The following (for example)
>
> template<class T>
> struct ptr
> {
> T* v;
> ptr(T* _v) : v(_v) {}
> T& operator*() const throw() { return *v; }
> };
>
> int main(int argc, char* argv[])
> {
> ptr<char> nullp(NULL);
> char& pc=*nullp;
> char c=pc;
> return 0;
> }
>
> crashes, as expected, in the line "char c=pc" (MSVC6).
>
> Is the throw() declaration meaningful, since some hidden paragraph in the
> standard allows a compiler to throw if he detects the construction of a null
> reference?

Obviously your question makes no sens in theory.
Should such throw() declaration be meaningful ? Dunno.

> I haven't seen any instructions yet, that 'undefined' operations shall throw
> exceptions.

In MSVC it's what happens. (Not C++ exceptions, but some kind
of OS-specific C++-independant exceptions.)

The interaction in MSVC (or in an ideal world) with C++ ES
(exception specs) isn't very clear to me.

> Couldn't there be an option to detect them? Like:
>
> T& operator *() const throw()
> {
> #if BOOST_DETECT_UNDEFINED_OPS
> if (v==NULL) throw undefined_behaviour();
> #endif
> return *v;
> }

Yes, this can be usefull for debuging.

Is there any compiler that can detect such dereferences (of null
pointers) ? (lcc can, b it's only a C compiler.)

Is it the right forum to discuss compiler generated exceptions ?
Probably not. comp.std.c++ is certainly the right place.

-- 
Valentin Bonnard

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