llvm / llvm/llvm-project

[Clang] `reference_constructs_from_temporary` mishandles cv-qualified arrays

Open
#198,580 4 comments 0 reactions 0 assignees View on GitHub
clang:frontend
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

For the following test case, the MSVC Compiler 19.52 Preview reports `false` while Clang 22.1.3 reports `true`. We believe that Clang is wrong:

(Click to expand version info)

```
C:\Temp>"%ProgramFiles%\Microsoft Visual Studio\18\Insiders\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=preview
**********************************************************************
** Visual Studio 2026 Developer Command Prompt v18.7.0-insiders
** Copyright (c) 2026 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Temp>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.52.36405.1 for x64 (PREVIEW)
Copyright (C) Microsoft Corporation. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

C:\Temp>clang-cl -v
clang version 22.1.3 (https://github.com/llvm/llvm-project e9846648fd6183ee6d8cbdb4502213fcf902a211)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\Microsoft Visual Studio\18\Insiders\VC\Tools\Llvm\x64\bin
```

```
C:\Temp>type reduced.cpp
```
```cpp
#include
#include
using namespace std;

int main() {
using Obj = int[1];
constexpr bool val = reference_constructs_from_temporary_v;

#ifdef __clang__
println("Clang: {}", val);
#else
println("MSVC: {}", val);
#endif
}
```
```
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest reduced.cpp && reduced
reduced.cpp
MSVC: false

C:\Temp>clang-cl /EHsc /nologo /W4 /std:c++latest reduced.cpp && reduced
Clang: true
```

Why is Clang wrong? See https://godbolt.org/z/8erKjbjsf which shows that all compilers (GCC 16, Clang 22, MSVC 19.50) agree that this is ill-formed:

```cpp
int main() {
using Obj = int[1];
using U = volatile Obj;
const Obj& p(U{});
}
```

[N5046](https://isocpp.org/files/papers/N5046.pdf) \[tab:meta.unary.prop\] specifies `reference_constructs_from_temporary` as "`T` is a reference type, and the initialization `T t(VAL);` is well-formed and binds `t` to a temporary object whose lifetime is extended (6.8.7)."

Because this initialization isn't well-formed, the type trait must report `false`. Note that GCC 16 behaves like MSVC and reports `false`, see https://godbolt.org/z/6fbTsnona .

Likely related to #142554 which implemented [LWG-3819](https://cplusplus.github.io/LWG/issue3819) "`reference_meows_from_temporary` should not use `is_meowible`".

Contributor guide

Open the contributing guide

Research direction

Start by running the reduced.cpp reproducer and compare Clang's result with the expected false value and the GCC/MSVC behavior shown. Then inspect the implementation associated with #142554 and LWG-3819; done means the trait reports false for the cv-qualified array case without regressing related cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.