Topic: Exception Specs in typedefs


Author: smeyers@teleport.com (Scott Meyers)
Date: 1996/07/22
Raw View
I originally posted the following on April 30, but as far as I know,
there was no followup.  I'm quite interested in this issue, because I
use an exception spec in a typedef in "More Effective C++," and the
probibition against this practice means that the book has a bug.

I'm hoping that somebody on the standardization committee will post a
note to the effect that, "Heck, once we decided to add the 'export'
keyword and we decided that the return value optimization would also
apply to named objects, we got carried away and decided to lift the
prohibition on exception specs in typedefs, too."

Pretty please?

Thanks,

Scott


Paul D. DeRocco <pderocco@ix.netcom.com> wrote:
| Jack Reeves wrote:
| > Can anyone explain why the DWP explicity disallows exception
| > specifications on a typedef?
|
| Because it's not part of the type. And a good thing too, since exception
| specs are far more likely to change in a library routine than the actual
| arg and return types.

Exception specs may not technically be part of a function's type, but note
that they are checked in many cases.  From DWP 15.4:

  2 If  any  declaration of a function has an exception-specification, all
    declarations, including the definition, of that function shall have an
    exception-specification  with  the same set of type-ids.  If a virtual
    function has an exception-specification, all  declarations,  including
    the  definition,  of any function that overrides that virtual function
    in any derived class shall have an exception-specification at least as
    restrictive as that in the base class.  [Example:
            struct B {
                virtual void f() throw (int, double);
                virtual void g();
            };

            struct D: B {
                void f();                    // ill-formed
                void g() throw (int);        // OK
            };
      --end  example]  The  declaration  of  D::f is ill-formed because it
    allows all exceptions, whereas B::f allows only int and double.  Simi-
    larly,  any function or pointer to function assigned to, or initializ-
    ing, a pointer to function shall have  an  exception-specification  at
    least as restrictive as that of the pointer or function being assigned
    to or initialized.  [Example:
            void (*pf1)();    // no exception specification
            void (*pf2) throw(A);

            void f()
            {
                    pf1 = pf2;  // ok: pf1 is less restrictive
                    pf2 = pf1;  // error: pf2 is more restrictive
            }
     --end example]

  3 In such an assignment or initialization,  exception-specifications  on
    return types and parameter types shall match exactly.

  4 In  other  assignments  or  initializations,  exception-specifications
    shall match exactly.

Given that the syntax of function pointers is one of the best arguments for
typedefs and given that exception specs are checked during initialization
and assignment of function pointers, the explicit restriction against the
use of exception specs in typedefs seems almost cruel.  Unless there is a
better argument against allowing exception specs in typedefs, I think the
prohibition should be reconsidered.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]