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

Add constexpr support to allocators for C++20 #603

Closed
wants to merge 1 commit into from

Conversation

jeaye
Copy link
Contributor

@jeaye jeaye commented Dec 28, 2023

This adds the constexpr attribute on each allocator member function, including ctors and dtors, to enable easier use of these allocators in compile-time execution. A practical example of this would be a modern C++ string class which supports constexpr construction, usage, and destruction. Without this change, the C++ compiler will not allow such a class.

An trivial example to show the usage:

// Compile with: c++ -std=c++20 -Iinclude test.cpp
struct string
{
  constexpr string() = default;
  constexpr ~string() = default;

  gc_allocator<char> a;
};

int main()
{ }

This fails to compile before this commit and succeeds afterward. I am using these changes in practice within jank's 0 compiler and runtime here 1.

Note that constexpr itself was added in C++11, but it can't apply to destructors until C++20, and it's coming on 2024, so I've skipped over trying to juggle the different levels of constexpr here by just saying if you have C++20, you can do what we need.

This adds the `constexpr` attribute on each allocator member function,
including ctors and dtors, to enable easier use of these allocators in
compile-time execution. A practical example of this would be a modern
C++ string class which supports constexpr construction, usage, and
destruction. Without this change, the C++ compiler will not allow
such a class.

An trivial example to show the usage:

```cpp

struct string
{
  constexpr string() = default;
  constexpr ~string() = default;

  gc_allocator<char> a;
};

int main()
{ }
```

This fails to compile before this commit and succeeds afterward. I am
using these changes in practice within jank's [0] compiler and runtime
here [1].

Note that `constexpr` itself was added in C++11, but it can't apply to
destructors until C++20, and it's coming on 2024, so I've skipped over
trying to juggle the different levels of `constexpr` here by just saying
if you have C++20, you can do what we need.

[0]: https://github.com/jank-lang/jank
[1]: https://github.com/jank-lang/jank/pull/50/files#diff-e0a05b8607cd30211eacb4bc222986254ed8af3942ae602092ca3dbd150121d4
@ivmai
Copy link
Owner

ivmai commented Dec 29, 2023

Looks good!

@jeaye
Copy link
Contributor Author

jeaye commented Dec 29, 2023

Nice, glad to hear it. Thanks for the quick review. Ready when you are.

ivmai pushed a commit that referenced this pull request Dec 31, 2023
PR #603 (bdwgc).

This adds the `constexpr` attribute on each allocator member function,
including ctors and dtors, to enable easier use of these allocators in
compile-time execution.  A practical example of this would be a modern
C++ string class which supports constexpr construction, usage, and
destruction.  Without this change, the C++ compiler will not allow
such a class.

* include/gc/gc_allocator.h (gc_allocator, traceable_allocator,
gc_allocator_ignore_off_page): Declare all member functions as
GC_CONSTEXPR; reformat code.
* include/gc/gc_config_macros.h [__cplusplus && !GC_CONSTEXPR]
(GC_CONSTEXPR): Define macro.
@ivmai
Copy link
Owner

ivmai commented Dec 31, 2023

Merged. Thank you!

@ivmai ivmai closed this Dec 31, 2023
ivmai pushed a commit that referenced this pull request Feb 2, 2024
(a cherry-pick of commit 9702965 from 'master')

PR #603 (bdwgc).

This adds the `constexpr` attribute on each allocator member function,
including ctors and dtors, to enable easier use of these allocators in
compile-time execution.  A practical example of this would be a modern
C++ string class which supports constexpr construction, usage, and
destruction.  Without this change, the C++ compiler will not allow
such a class.

* include/gc_allocator.h (gc_allocator, traceable_allocator,
gc_allocator_ignore_off_page): Declare all member functions as
GC_CONSTEXPR; reformat code.
* include/gc_config_macros.h [__cplusplus && !GC_CONSTEXPR]
(GC_CONSTEXPR): Define macro.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants