Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How constexpr vector works in C++20 #418

Open
lefticus opened this issue Aug 7, 2024 · 0 comments
Open

How constexpr vector works in C++20 #418

lefticus opened this issue Aug 7, 2024 · 0 comments

Comments

@lefticus
Copy link
Owner

lefticus commented Aug 7, 2024

This is effectively what you have to do to do constexpr vector-like things. Placement new is not yet constexpr (C++26) and you cannot change pointer types at compile time until C++26 too.

So you cannot just "new" a bunch of bytes then expect to use them.

#include <string>

constexpr void f()
{
  std::allocator<std::string> allocator;
  auto *ptr = allocator.allocate(10);
  std::construct_at(ptr, "Hello a long string");
  std::construct_at(ptr + 1, "World");
  std::destroy_at(ptr);
  std::destroy_at(ptr+1);
  allocator.deallocate(ptr, 10);
}

int main()
{
  [] consteval { f(); }();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant