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

Fix emplace back implementations #418

Merged
merged 4 commits into from
Jul 10, 2024
Merged

Conversation

Adda0
Copy link
Collaborator

@Adda0 Adda0 commented Jul 9, 2024

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 existing operator==().

Merging this PR fixes #331.

@Adda0 Adda0 requested a review from jurajsic July 9, 2024 10:05
Comment on lines 84 to 89
template <typename... Args>
StateSet& emplace_back(Args&&... args) {
return targets.emplace_back(std::forward<Args>(args)...);
}

Copy link
Member

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.

Copy link
Collaborator Author

@Adda0 Adda0 Jul 9, 2024

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).

Copy link
Member

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".

Copy link
Collaborator Author

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.

@Adda0 Adda0 force-pushed the fix_emplace_back_implementations branch from fdd668a to acea43b Compare July 10, 2024 06:00
@Adda0
Copy link
Collaborator Author

Adda0 commented Jul 10, 2024

As all issues have been resolved, I will merge the PR once the tests succeed.

@Adda0 Adda0 merged commit bf1a001 into devel Jul 10, 2024
18 checks passed
@Adda0 Adda0 deleted the fix_emplace_back_implementations branch July 10, 2024 07:26
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.

Correct emplace_back for all containers
2 participants