[cfe-dev] -Wcatch-incomplete-type-extensions

Howard Hinnant hhinnant at apple.com
Wed Jan 18 18:01:01 PST 2012


I'm wondering why this is a warning and not an error:

struct A;

void foo();

#include <iostream>

int main()
{
    try
    {
        foo();
    }
    catch (A&)
    {
        std::cout << "Caught an A\n";
    }
    catch (...)
    {
        std::cout << "Caught ...\n";
    }
}

struct A {};

void foo()
{
    throw A();
}

test.cpp:13:14: warning: ISO C++ forbids catching a reference to incomplete type 'A' [-Wcatch-incomplete-type-extensions]
    catch (A&)
             ^
test.cpp:1:8: note: forward declaration of 'A'
struct A;
       ^
1 warning generated.

g++-4.2 just says:

test.cpp: In function ‘int main()’:
test.cpp:13: error: invalid use of incomplete type ‘struct A’
test.cpp:1: error: forward declaration of ‘struct A’

15.3 [except.handle]/p1 says:

... The exception-declaration shall not denote an incomplete type, ...

There is no mention of "no diagnostic is required" in that paragraph.  Furthermore 1.4 [intro.compliance]/p2/b2 says:

	• If a program contains a violation of any diagnosable rule or <...> a conforming implementation shall issue at least one diagnostic message.

And  1.4 [intro.compliance]/p1 says:

	• The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior.”

Strictly speaking clang looks conforming to me unless the -Wcatch-incomplete-type-extensions warning has been turned off.  But the spirit of the rule seems to me that this should be an error not a warning.

The C++ runtime is responsible for dealing with the catch clause and matching the thrown object to the type in the catch handler.  The gcc library simply crashes on Apple's OS Lion:

Bus error: 10

Is there any rationale for making this a warning instead of a hard error?  This question is pertinent to my work in our libc++abi project (http://libcxxabi.llvm.org/).  I.e. should I spend extra run time cycles to handle this situation more gracefully (e.g. call terminate())?  Or should I be assured that the compiler will make sure this can't happen?

Howard





More information about the cfe-dev mailing list