xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

Template Instantiation for xtensor_fixed

Open kaigu1997 opened this issue 3 years ago • 0 comments

I tried different ways to set the parameters for xtensor_fixed, as in function e, f, g and h in the following code

#include <type_traits>
#include <xtensor.hpp>

static constexpr std::size_t N = 3;

template <std::size_t ... xIndices, std::size_t ... pIndices>
static inline auto e(const xt::xtensor_fixed<double, xt::xshape<xIndices ..., N>>& x,
	const xt::xtensor_fixed<double, xt::xshape<pIndices ..., N>>& p) -> std::decay_t<decltype(p)>
{
	return x + p;
}

template <std::size_t ... xIndices, std::size_t ... pIndices>
static inline auto f(const xt::xtensor_fixed<double, xt::xshape<xIndices ...>>& x,
	const xt::xtensor_fixed<double, xt::xshape<pIndices ...>>& p) -> std::decay_t<decltype(p)>
{
	return x + p;
}

template <typename xShape, typename pShape>
static inline auto g(const xt::xtensor_fixed<double, xShape>& x,
	const xt::xtensor_fixed<double, pShape>& p) -> std::decay_t<decltype(p)>
{
	return x + p;
}

template <typename xType, typename pType>
static inline auto h(const xType& x,
	const pType& p) -> std::decay_t<decltype(p)>
{
	return x + p;
}

int main()
{
	const xt::xtensor_fixed<double, xt::xshape<N>> x0 = xt::arange(N), p0 = xt::arange(N);
	const xt::xtensor_fixed<double, xt::xshape<N, N>> p1 = xt::zeros<double>({N, N});
	const auto xe1 = e(x0, p0);
	const auto xe2 = e(x0, p1);
	const auto xf1 = f(x0, p0);
	const auto xf2 = f(x0, p1);
	const auto xg1 = g(x0, p0);
	const auto xg2 = g(x0, p1);
	const auto xh1 = h(x0, p0);
	const auto xh2 = h(x0, p1);
	static_assert(std::is_same_v<decltype(x0), decltype(xe1)>);
	static_assert(std::is_same_v<decltype(x0), decltype(xe2)>);
	static_assert(std::is_same_v<decltype(x0), decltype(xf1)>);
	static_assert(!std::is_same_v<decltype(x0), decltype(xf2)>);
	static_assert(std::is_same_v<decltype(p1), decltype(xf2)>);
	static_assert(std::is_same_v<decltype(x0), decltype(xg1)>);
	static_assert(!std::is_same_v<decltype(x0), decltype(xg2)>);
	static_assert(std::is_same_v<decltype(p1), decltype(xg2)>);
	static_assert(std::is_same_v<decltype(x0), decltype(xh1)>);
	static_assert(!std::is_same_v<decltype(x0), decltype(xh2)>);
	static_assert(std::is_same_v<decltype(p1), decltype(xh2)>);
}

Under ubuntu22.04 (gcc 11.02+xtensor 0.23.10), the above code compiled successfully, which indicates the instantiation of e for x0 and p1 is not what I expected and thus wrong. Why?

kaigu1997 avatar Jul 06 '22 22:07 kaigu1997