I have no idea what's wrong with this snippet. I'm getting this error:
error: member function 'swap' not viable: 'this' argument has type 'const array', but function is not marked const
#include <algorithm>
#include <memory>
#include <iostream>
#include <array>
struct MyClass {
std::array<float, 4> arr;
float carr[4];
std::array<float, 4> getArrElement() {
std::array<float, sizeof(carr) / sizeof(float)> out;
return out;
}
void fun() {
auto vec = { getArrElement(), getArrElement(), getArrElement() };
std::reverse(vec.begin(), vec.end()); // <-- error line here
}
};
int main()
{
MyClass obj;
obj.fun();
}
getArrElement
isn't returning a const
array. auto
should be deduced to std::initializer_list
but I also see no harm in that.
What's wrong?
As above, plus if the array is only used locally and never truncated, extended or moved, then by using the free functions begin()
and end()
we can avoid the overhead of allocating a vector....
using element_type = decltype(std::declval<MyClass>().getArrElement());
element_type vec[] = { getArrElement(), getArrElement(), getArrElement() };
std::reverse(std::begin(vec), std::end(vec));