-
Notifications
You must be signed in to change notification settings - Fork 13
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
Fix emplace back implementations #418
Conversation
include/mata/nfa/delta.hh
Outdated
template <typename... Args> | ||
StateSet& emplace_back(Args&&... args) { | ||
return targets.emplace_back(std::forward<Args>(args)...); | ||
} | ||
|
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 does this do? Or how is this thing called? Because for me this is C++ dark magic.
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.
It is a dark C++ magic, but in this case still an understandable one. It is called variadic templates, i.e., a template function with a variable number of arguments of the template type. We need this for the emplace_back()
functions since the purpose of the emplace_back()
is to accept a variable number of arguments, and try to construct an object of the type of the elements in the container with the passed arguments. That can work with zero, one, two or more arguments, each version constructing an object through possibly a different constructor of that type.
So we accept a variable number of arguments, but we do not want to construct the object in this function (in its scope) as the construction should only happen on the underlying vector targets
in place. That means that we need to take these arguments without unpacking them or in any way resolving the pack of arguments. Instead, we directly pass them along (std::forward
as the same pack of type Args
) to another function taking a variadic pack of arguments, in this case the standard std::vector<T>::emplace_back()
, which has the same signature as this function. Only now the standard method already performs the allocation of the space in the underlying vector, calls a constructor of the underlying type of the elements of the vector and so on. Only there will the pack be unpacked, and the arguments will be tried against all possible constructors for the underlying type of the elements as if you were calling the constructors manually as TypeConstructor(arg1, arg2, ..., argn)
.
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 would put some comment here, something like "forwarding variadic templates to std::vector implementation".
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.
The comment has been added. It should be easier to look the relevant information up if necessary.
fdd668a
to
acea43b
Compare
As all issues have been resolved, I will merge the PR once the tests succeed. |
This PR fixes the implementations of
emplace_back()
methods throughout the Mata library to correctly construct the object in-place directly at the end of the containers.Furthermore, this PR removes the redundant implementations of operators which are automatically generated by the compiler such as
operator!=()
for the existingoperator==()
.Merging this PR fixes #331.