考虑以下代码:
#include <chrono>
#include <iostream>
#include "xtensor/xadapt.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xindex_view.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
using namespace std;
using namespace std::chrono;
int main() {
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i = 0; i < 1000; i++) {
xt::expression<float> a = xt::zeros<float>({3, 256, 256});
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time = duration_cast<duration<double>>(t2 - t1);
cout << time.count() << endl;
}这无法编译,并出现以下错误:test.cpp:24:66: error: ‘xt::xexpression<D>::~xexpression() [with D = float]’ is protected within this context。当将xt::xexpression<float> (the correct return type)更改为auto时,它会编译并运行。如果xt::expression是受保护的,为什么auto可以访问它?有没有一种方法可以指定类型而不是使用auto,而不需要计算xexpression?(例如,我不能让a的类型为xt::xarray,因为这会强制求值)。
发布于 2021-07-28 00:47:21
正如凯文在评论中指出的那样,Auto并没有解析到xexpression,而是xexpression的孩子。将类型更改为xt::xbroadcast<xt::xscalar<float>, std::array<long unsigned int, 3> >将在不计算的情况下进行编译。
https://stackoverflow.com/questions/68548036
复制相似问题