为什么在模板函数中将两个xtensor表达式加在一起会广播不正确?

时间:2018-07-28 01:46:31

标签: c++ templates xtensor

考虑以下程序:

#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
xt::xarray<double> arr1
  {1.0, 2.0, 3.0};

xt::xarray<double> arr2
  {5.0, 6.0, 7.0};

template <typename T, typename U>
struct container{
    container(const T& t, const U& u) : a(t), b(u) {}
    T a;
    U b;
};

template <typename T, typename U>
container<T, U> make_container(const T& t, const U& u){
    return container<T,U>(t, u);
}

auto c = make_container(arr1, arr1);
std::cout << (arr1 * arr1) + arr2;

template <typename A, typename B, typename R>
auto operator+(const container<A, B>& e1, const R& e2){
    return (e1.a * e1.b) + e2;
}

std::cout << (c + arr2);

如果我们看一下代码:

std::cout << (arr1 * arr1) + arr2;

它将输出:

{  6.,  10.,  16.}

但是,运行最后一行:

std::cout << (c + arr2);

满足以下条件:

{{  6.,   9.,  14.}, {  7.,  10.,  15.}, {  8.,  11.,  16.}}

为什么会这样?我将operator+的函数定义更改为以下内容:

template <typename A, typename B, typename R>
auto operator+(const container<A, B>& e1, const R& e2){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    return (e1.b * e1.alpha) + e2;
}

输出令人惊讶:

auto operator+(const container<A, B> &, const R &) [A = xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, B = xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, R = double]
auto operator+(const container<A, B> &, const R &) [A = xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, B = xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, R = double]
auto operator+(const container<A, B> &, const R &) [A = xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, B = xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, R = double]
{{  6.,   9.,  14.}, {  7.,  10.,  15.}, {  8.,  11.,  16.}}

为什么单个操作中要调用3个+操作?是否在某个地方定义了导致此行为的宏? R中的operator+类型为我们提供double,实际上应该为xt::xarray<double>

感谢您提供任何见解。

1 个答案:

答案 0 :(得分:2)

在命名空间operator+中定义的xt具有通用引用,因此在编写c + arr2时优先于重载。

因此,最后一行将返回一个xfunction,其第一个操作数是您的container,第二个操作数是一个xarray

现在,由于container不是xexpression,因此在xfunction内部将其视为... xscalar<container>

因此,当您尝试访问此xfunction的第i个元素时,将执行以下操作:xscalar<container> + arr2[i](广播xscalar)。由于xscalar<container>可转换为container,因此您的operator+重载将以R解析为value_type的{​​{1}},即{{ 1}}。

以下循环说明了此行为:

arr2

它将生成您的double的以下调用:

auto f = c + arr2;
for(auto iter = f.begin(); iter != f.end(); ++iter)
{
    std::cout << *iter << std::endl;
}

这就是为什么您看到operator+的3个呼叫的原因。