Richard Hodges
Richard Hodges

Reputation: 69892

Is this compiler bug triggered by invalid code, or should it compile?

Given the following complete program:

#include <functional>

struct jobbie
{
    std::function<void()> a;
};

void do_jobbie(jobbie j = {})
{
    if (j.a) 
        j.a();
}

int main()
{
    do_jobbie();
    return 0;
}

compiling this on gcc (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1: boom!

richard@DEV1:~/tmp$ g++ -std=c++11 bug.cpp
bug.cpp: In function ‘int main()’:
bug.cpp:16:13: internal compiler error: in create_tmp_var, at gimplify.c:479
  do_jobbie();
             ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions.
Preprocessed source stored into /tmp/ccWGpb7M.out file, please attach this to your bugreport.

However, clang [Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)] is happy with it.

$ clang++ -std=c++11 bug.cpp

It seems to me that clang is correctly deducing that j defaults to a default-constructed jobbie object whereas gcc is (obviously) blowing up.

replacing line 8 with void do_jobbie(jobbie j = jobbie {}) fixes the problem on gcc.

Question - which of these is true:

  1. clang is correct, gcc is faulty (ignoring the compiler blow-up)
  2. clang is over-reaching the standard and it should not really compile
  3. the standard does not make it clear?

Upvotes: 2

Views: 441

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283684

This indicates that it should work:

The default argument has the same semantic constraints as the initializer in a declaration of a variable of the parameter type, using the copy-initialization semantics.

(8.3.6, wording from draft n3936)

Upvotes: 2

bames53
bames53

Reputation: 88175

An internal compiler error is always a compiler bug. Compilers should be able to process anything without crashing like that.

clang has similar handling for when it crashes, producing data for reporting the bug and pointing the user to clang's bug reporting web page.

I don't see anything tricky about this code. It seems straightforward to me that it should compile and run.

Upvotes: 4

Related Questions