Skip to content

[Clang] reference_constructs_from_temporary mishandles cv-qualified arrays #198580

@StephanTLavavej

Description

@StephanTLavavej

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
#include <print>
#include <type_traits>
using namespace std;

int main() {
    using Obj          = int[1];
    constexpr bool val = reference_constructs_from_temporary_v<const Obj&, volatile Obj>;

#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:

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

N5046 [tab:meta.unary.prop] specifies reference_constructs_from_temporary<T, U> as "T is a reference type, and the initialization T t(VAL<U>); 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 "reference_meows_from_temporary should not use is_meowible".

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions