llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.03k stars 11.58k forks source link

clang's compound-literal-in-C++ extension behaves differently from GCC's when initializing a reference #36519

Open llvmbot opened 6 years ago

llvmbot commented 6 years ago
Bugzilla Link 37171
Version unspecified
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

The test code is as follow:

class C {}
a = (const C &) {};

clang++ produces an error message:

1testcase.cpp:2:5: error: reference to type 'const C' cannot bind to an
      initializer list
a = (const C &) {};
    ^           ~~
1 error generated.

However, g++ compiles the above code without any errors. Is this a problem of clang or gcc?

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 6 years ago

However, g++ compiles the above code without any errors. Is this a problem of clang or gcc?

No, this is a language extension in both compilers, and the two compilers have somewhat different extensions. Clang tries to make its C++ compound-literal extension match C compound-literals, whereas GCC defines (T){...} as being the same as T{...}.

However, being more GCC-compatible here seems like it might be a good thing, since C doesn't have references.

llvmbot commented 2 months ago

@llvm/issue-subscribers-clang-frontend

Author: None (llvmbot)

| | | | --- | --- | | Bugzilla Link | [37171](https://llvm.org/bz37171) | | Version | unspecified | | OS | Linux | | Reporter | LLVM Bugzilla Contributor | | CC | @DougGregor,@zygoloid | ## Extended Description The test code is as follow: ```cpp class C {} a = (const C &) {}; ``` clang++ produces an error message: ``` 1testcase.cpp:2:5: error: reference to type 'const C' cannot bind to an initializer list a = (const C &) {}; ^ ~~ 1 error generated. ``` However, g++ compiles the above code without any errors. Is this a problem of clang or gcc?