[llvm-bugs] [Bug 48606] New: Clang rejects creation of struct with mutable member during constant evaluation
    via llvm-bugs 
    llvm-bugs at lists.llvm.org
       
    Sun Dec 27 05:36:45 PST 2020
    
    
  
https://bugs.llvm.org/show_bug.cgi?id=48606
            Bug ID: 48606
           Summary: Clang rejects creation of struct with mutable member
                    during constant evaluation
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mschellenbergercosta at googlemail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk
Consider the following code with C++20:
```
#include <memory>
struct WithMutable {
    mutable int val_{0};
};
constexpr bool createOnStack() {
    [[maybe_unused]] volatile WithMutable var;
    return true;
}
constexpr bool createOnHeap() {
    WithMutable* var = new WithMutable{};
    delete var;
    return true;
}
constexpr bool createWithAllocator() {
    WithMutable* var = std::allocator<WithMutable>{}.allocate(1);
    std::construct_at(var); 
    std::destroy_at(var);   
    std::allocator<WithMutable>{}.deallocate(var, 1);
    return true;
}
int main() {
    static_assert(createOnStack());
    static_assert(createOnHeap());
    static_assert(createWithAllocator());
}
```
Clang trunk rejects the last function `createWithAllocator` with the error
message: 
    "construction of mutable member 'val_' is not allowed in a constant
expression"
However, I cannot find any restriction on construction of mutable members
during a constant expression. Given that the other two functions are accepted
it is unclear why clang rejects the call to std::construct_at
(Godbolt link: https://godbolt.org/z/71zb7j)
-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201227/6cdd9241/attachment.html>
    
    
More information about the llvm-bugs
mailing list