[英]xtensor - Tensor wrapper raises trivial_assigner error during runtime
我目前正在將xtensor用於應用程序,並且我想包裝張量以創建一個名為BitArray
的類。
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
#include "xtensor/xindex_view.hpp"
xt::xarray<double> arr {1, 2, 3};
template <typename E>
class BitArray{
public:
BitArray(const xt::xexpression<E>& _array, float _alpha) :
b(xt::cast<std::int8_t>(_array.derived_cast())), alpha(_alpha) {}
xt::xarray<E> b;
float alpha;
};
template <class E>
auto make_bitarray(xt::xexpression<E>& expr, float alpha)
{
return BitArray<E>(expr, alpha);
}
auto a = make_bitarray(arr, 3); // Error
我收到以下錯誤消息:
Standard Exception: Precondition violation!
Internal error: trivial_assigner called with unrelated types.
/srv/conda/include/xtensor/xassign.hpp(505)
這是什么意思,我該怎么解決?
稍微更好的解決方案是在您的make_bitarray
函數內部創建演員make_bitarray
:
template <typename T>
class BitArray{
public:
BitArray(T&& _array, float _alpha) :
b(std::move(_array)), alpha(_alpha)
{
}
T&& b;
float alpha;
};
template <class T>
auto make_bitarray(const xt::xexpression<T>& expr, float alpha)
{
auto cast = xt::cast<int8>(expr);
// need to move temporary here
return BitArray<decltype(cast)>(std::move(expr), alpha);
}
這是我為復雜的模板參數提供包裝的步驟:
template <typename T>
class BitArray{
public:
BitArray(const xt::xexpression<T>& _array, float _alpha) :
b(xt::cast<int8>(_array.derived_cast())), alpha(_alpha)
{
}
decltype(xt::cast<int8>(std::declval<std::add_lvalue_reference_t<T>>())) b;
float alpha;
};
template <class T>
auto make_bitarray(const xt::xexpression<T>& expr, float alpha)
{
return BitArray<T>(expr, alpha);
}
添加一個左值,因為我檢查了唯一缺少的是一個ref,並且需要derived_cast
才能具有value_type
,然后需要將其包裝在xexpression
以確保可以對其求值,並對此進行decltype
給你答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.