Skip to content

Commit

Permalink
In array cross-construction invoke explicit ValueType conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Sep 17, 2024
1 parent 4ad0c2a commit 95619b3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions c++/nda/basic_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,15 @@ namespace nda {
* @param a nda::ArrayOfRank object.
*/
template <ArrayOfRank<Rank> A>
requires(HasValueTypeConstructibleFrom<A, value_type>)
requires(HasValueTypeConstructibleFrom<A, ValueType>)
basic_array(A const &a) : lay(a.shape()), sto{lay.size(), mem::do_not_initialize} {
static_assert(std::is_constructible_v<value_type, get_value_t<A>>, "Error in nda::basic_array: Incompatible value types in constructor");
static_assert(std::is_constructible_v<ValueType, get_value_t<A>>, "Error in nda::basic_array: Incompatible value types in constructor");
if constexpr (std::is_trivial_v<ValueType> or is_complex_v<ValueType>) {
// trivial and complex value types can use the optimized assign_from_ndarray
assign_from_ndarray(a);
if constexpr (std::is_same_v<ValueType, get_value_t<A>>)
assign_from_ndarray(a);
else
assign_from_ndarray(nda::map([](auto const &val) { return ValueType(val); })(a));
} else {
// general value types may not be default constructible -> use placement new
nda::for_each(lay.lengths(), [&](auto const &...is) { new (sto.data() + lay(is...)) ValueType{a(is...)}; });
Expand Down

0 comments on commit 95619b3

Please sign in to comment.