Skip to content

libc++: constexpr std::string does not work (clang 17) #75755

Closed as not planned
Closed as not planned
@q66

Description

@q66

The following code:

#include <string>

constexpr std::string foo{"XX"};

int main() {
}

should compile, but instead it fails with:

test.cc:3:23: error: constexpr variable 'foo' must be initialized by a constant expression
    3 | constexpr std::string foo{"XX"};
      |                       ^~~~~~~~~
test.cc:3:23: note: pointer to subobject of heap-allocated object is not a constant expression
/usr/bin/../include/c++/v1/__memory/allocator.h:112:38: note: heap allocation performed here
  112 |             return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
      |                                      ^
1 error generated.

I haven't been able to find anything about this being broken, and available resources list the functionality as available since 15.

This is a musl/libc++/compiler-rt environment, on x86_64 architecture, but probably should not matter. The libc++ has hardened mode on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"duplicateResolved as duplicatelibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions