Here's a code that can't be compiled with any at least c+++11-conform compiler (tried at godbolt):
#include <valarray>
#include <vector>
#include <iostream>
class Whatever {
public:
int sliceSum(const std::vector<int>& nums, int k) const {
std::valarray<int> data(nums.size());
for (int i = 0; i < nums.size(); ++i) {
data[i] = nums[i];
}
std::cout << typeid(decltype(data[std::slice(0, k, 1)])).name() << std::endl;
return data[std::slice(0, k, 1)].sum();
}
};
class Matrix {
std::valarray<int> data;
int dim;
public:
Matrix(int r, int c) : data(r*c), dim(c) {}
int& operator()(int r, int c) {return data[r*dim + c];}
int trace() const {
std::cout << typeid(decltype(data[std::slice(0, dim, 1)])).name() << std::endl;
return data[std::slice(0, dim, 1)].sum();
}
};
int main()
{
Matrix m(3,3);
int n = 0;
for(int r=0; r<3; ++r)
for(int c=0; c<3; ++c)
m(r, c) = ++n;
Whatever s;
s.sliceSum({1,2,3}, 3);
m.trace();
}
Class Matrix
is taken from https://en.cppreference.com/w/cpp/numeric/valarray/slice
and class Whatever
is mine.
So I don't understand what the problem is and why types are different in two identic (?) cases.
Aucun commentaire:
Enregistrer un commentaire