xtensor-stack / xtl

The x template library
BSD 3-Clause "New" or "Revised" License
207 stars 96 forks source link

Fix promote_type with two complex floats and one double #265

Closed mnijhuis-tos closed 1 year ago

mnijhuis-tos commented 1 year ago

While using xt::allclose on two objects containing std::complex<float>, I got an error from xtl::promote_type<std::complex<float>, std::complex<float>, double> that it tried determining the type when adding a std::complex<float> to a std::complex<double>, which is not defined.

When evaluating xtl::promote_type<std::complex<float>, std::complex<float>, double>, xtl indeed first recursively evaluates xtl::promote_type<std::complex<float>, double>, which yields a std::complex<double>. Then it determines the type of adding std::complex<float> and std::complex<double> using decltype and declval, which fails.

This fix avoids the addition operator in recursive calls of the promote_type template by generalizing the existing recursive template that already uses a bool as a first argument.

I also added a test that fails to compile without the fix and works fine with the fix.

I see that my editor removed some spaces and a final newline. Please let me know if that's a problem.

JohanMabille commented 1 year ago

Thanks for this fix!