-
Notifications
You must be signed in to change notification settings - Fork 8
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
Constexpr-ify inplace_vector #21
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I basically agree with your changes. Most of my remarks (and some serious bugs) are in the code you didn't change (some even where GitHub doesn't allow me to put a comment).
Not sure how to handle this. Maybe it's good when your PR gets to the main line and then we can work on the rest.
T *begin() { return std::launder(reinterpret_cast<const T *>(elems)); } | ||
const T *begin() const { | ||
return std::launder(reinterpret_cast<const T *>(elems)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason you are using std::launder
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to be consistent with previous implementation, I am bit confused myself too. I am not in a good place to make judgement call here, though I suspect it might be needed when T is const.
I don't think we should change this.
internal_size_type size_{0}; | ||
|
||
// [containers.sequences.inplace.vector.cons], construct/copy/destroy | ||
constexpr inplace_vector_destruct_base() = default; | ||
|
||
inplace_vector_destruct_base( | ||
constexpr inplace_vector_destruct_base( | ||
const inplace_vector_destruct_base | ||
&other) noexcept(std::is_nothrow_copy_constructible_v<T>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your change, but to me this is unconditionally noexcept, since no T's are copied at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay I will mark a TODO here. There's a lot of non-conformity in this implementation.
I think we should defer this to another PR, there's too much to be updated.
std::is_nothrow_copy_constructible_v<T> && | ||
std::is_nothrow_copy_assignable_v<T>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your change, but to me this is unconditionally noexcept, since no T's are copied at all.
std::is_nothrow_move_constructible_v<T> && | ||
std::is_nothrow_move_assignable_v<T>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your change, but to me this is unconditionally noexcept, since no T's are copied at all.
internal_size_type size_{0}; | ||
|
||
// [containers.sequences.inplace.vector.cons], construct/copy/destroy | ||
constexpr inplace_vector_destruct_base() = default; | ||
|
||
inplace_vector_destruct_base( | ||
constexpr inplace_vector_destruct_base( | ||
const inplace_vector_destruct_base | ||
&other) noexcept(std::is_nothrow_copy_constructible_v<T>) | ||
: elems(), size_(other.size_) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your change, but I need to make some remark on all constructors in inplace_vector_destruct_base
.
IMO, it would be O.K. to address this in a later PR.
They all value-initialise elems
, i.e. the bytes in bytes_based_Storage become 0 and all elements in type_based_storage are value initialised. I tried to argue above why I don't like this, they should be initialised only when a vector element is really constructed in the place.
Besides (superfluously) initialising elems
, they all do the same thing, they set size_
to some value. That is, they avoid doing what a copy c'tor, move c'tor etc. normally does and instead manipulate size_
only. This is redundant. Instead, a single constructor taking a size
would do. This could be called by the all the constructors in the deriving class. size_
could probably be made private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inplace_vector_destruct_base
needs more work on trivially destructible requirement as well, needs to switch between a trivially destructible base and a non-trivial destructor against T.
Unfortunately intention in this repo is to support C++17, so we cannot use requires. This have to be done with template.
std::is_nothrow_copy_constructible_v<T>) | ||
: inplace_vector_destruct_base<T, Capacity>(other.size) { | ||
: inplace_vector_destruct_base<T, Capacity>(other.size_) { | ||
std::copy(other.begin(), other.end(), begin()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your change, but needs to be an uninitialised_copy
, in particular for the byte based storage case.
It happens work in the type based storage case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is uninitialised_copy
needed here?
std::copy(other.begin(), other.end(), begin()); | ||
} | ||
inplace_vector_base(inplace_vector_base &&other) noexcept( | ||
constexpr inplace_vector_base(inplace_vector_base &&other) noexcept( | ||
Capacity == 0 || std::is_nothrow_move_constructible_v<T>) | ||
: inplace_vector_destruct_base<T, Capacity>(other.size_) { | ||
std::copy(other.begin(), other.end(), begin()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not your change, but must be uninitalized_move
, see above.
Thank you for review jbab. I agree that there's a lot of problems in this current implementation. That's why I am not attempting to setup a full test suite for constexpr. I think it will cause too big of a change. This is just my thoughts and I haven't really discussed this in beman. But I am reaching out to david to see if we should just adopt the reference implementation in the paper (if the original author allows). As there's a lot of work to be done here and the reference implementation will be a better place to be worked off. Edit: okay I figured this out myself... Here's the paper allowing default trivial initialization. I don't get why Previous confusionRegarding storage, mostly due to my own limited understanding, I am not sure how constexpr plays with default-initialized memory, I thought all value must be initialized to a defined state to play nice with constexpr. e.g. I thought func in the follow code is not constexpr because a is not initialized. constexpr int func() {
int a[5];
a[0] = 1;
return a[0];
} Which is false because this passes godbolt... However, in the paper, it mentions this wording:
Which suggest we need to value-initialize the T based array to make it play well with constexpr (use I mean compiler seem to be fine with it, I just want to make sure this is intended per standard. I tried to check with cppreference on constexpr, but I don't see how this is specified either way. Edit: |
Btw, it is likely we will need to update this storage container to include |
@jbab If you don't mind, can you post an issue regarding questionable restraints surrounding constructors. I will deal with them in another PR. |
Co-authored-by: Jan Babst <jan.babst@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Hey @jbab , I've been given the green light to adopt the reference implementation from the paper. I will be syncing that to a new branch and do a merge next. Let's scrutinize the reference implementation first, if we have time we can come back and make current implementation great |
Make
inplace_vector
constexpr
friendly by providing an T-based array storage alternative that avoids the needs ofreinterpret_cast
.This storage alternative is only selected when
std::is_trivial(T)
, as that's the requirement for enabling constexpr in the paper.Adds constexpr based tests.
Related sections in paper:
All member functions should be constexpr.
Added a constexpr test that is not finished, I fear the code-fix resulted due to passing constexpr on all functions would result in too big of a PR. Currently
[container.reqmts]
related requirement is tested and fixed for int type.Closes #17