llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang does not support initializing constant reference member with literal

Open Ming-3364 opened this issue 2 years ago • 2 comments

The following simple test is currently rejected by clang:

struct test {
  test() : x(0) {}
  const int &x;
} ;

Assigning a temporary value to a corresponding constant reference variable is allowed in C++. Accordingly, the test should be well-formed.

Both GCC and MSVC accept it:

https://godbolt.org/z/dP7h4dG63

Ming-3364 avatar Jul 31 '24 14:07 Ming-3364

@llvm/issue-subscribers-clang-frontend

Author: Raki (Ming-3364)

The following simple test is currently rejected by clang:
struct test {
  test() : x(0) {}
  const int &x;
} ;

Assigning a temporary value to a corresponding constant reference variable is allowed in C++. Accordingly, the test should be well-formed.

Both GCC and MSVC accept it:

https://godbolt.org/z/dP7h4dG63

llvmbot avatar Jul 31 '24 15:07 llvmbot

This example violates https://eel.is/c++draft/class.base.init#8; clang is correct to reject it.

zygoloid avatar Jul 31 '24 16:07 zygoloid