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 Attribute copy/move constructors #1545

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions include/openPMD/backend/Attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ class Attribute
std::vector<float>,
std::vector<double>,
std::vector<long double>,
std::vector<std::complex<float> >,
std::vector<std::complex<double> >,
std::vector<std::complex<long double> >,
std::vector<std::complex<float>>,
std::vector<std::complex<double>>,
std::vector<std::complex<long double>>,
std::vector<signed char>,
std::vector<std::string>,
std::array<double, 7>,
Expand All @@ -107,7 +107,14 @@ class Attribute
* Fix by explicitly instantiating resource
*/
template <typename T>
Attribute(T &&val) : Variant(resource(std::forward<T>(val)))
Attribute(std::enable_if_t<
// If T is `Attribute` or `Attribute const &`, this constructor
// should not be used, but instead the move/copy constructors
!std::is_same_v<
std::remove_cv_t<std::remove_reference_t<T>>,
Attribute>,
T> &&val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would specializing be simpler/cleaner than enable if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be better, yes. We would need to do this for every openPMD datatype then, though.
Probably best at that point to introduce something like the datatype macros that ADIOS2 has? openPMD_FOREACH_DATATYPE or so?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that needed? The specialization could be something like

template <typename T>
Attribute(Attribute<T> const &) = default;

template <typename T>
Attribute(Attribute<T> &&) = default;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template specialization doesn't work in this case: With template, we cannot use default, without template, it's not a specialization, but an overload that is never chosen.
Using a specialization doesn't fix the underlying problem. In the current form, the constructor template <typename T> Attribute(T &&val) accepts anything, but is supposed to work only for all openPMD datatypes. That's a wrong interface.
Since the Intel Compiler doesn't understand this use of enable_if, I'd suggest using a macro.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best would be a C++20 concept here though :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I propose is no template specialization but an overload for the constructors. Did you try if that works?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you did another approach with macros now ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I propose is no template specialization but an overload for the constructors. Did you try if that works?

That's exactly what this PR does. Just, it doesn't write all ~30 constructors explicitly, but uses a macro for that instead.

: Variant(resource(std::forward<T>(val)))
{}

/** Retrieve a stored specific Attribute and cast if convertible.
Expand Down
8 changes: 8 additions & 0 deletions test/CoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ TEST_CASE("attribute_dtype_test", "[core]")
{
Attribute a = Attribute(static_cast<char>(' '));
REQUIRE(Datatype::CHAR == a.dtype);
{
// check that copy constructor works
[[maybe_unused]] Attribute b = a;
}
{
// check that move constructor works
[[maybe_unused]] Attribute b = std::move(a);
}
a = Attribute(static_cast<unsigned char>(' '));
REQUIRE(Datatype::UCHAR == a.dtype);
a = Attribute(static_cast<short>(0));
Expand Down
Loading