|
|
|
|
|
by vitus
1422 days ago
|
|
I feel like a better comparison would be to use std::span (C++20) to mimic Rust's slice. Otherwise you might be tricked into thinking that adding // hey don't pass in a temporary
auto make_appender(std::vector<int>&& suffix) = delete;
(which turns the provided code into a compiler error under both g++ and clang++) is adequate to prevent the immediate class of issues (namely, C++ allows const Foo& and Foo&& to bind to temporaries).Meanwhile, it's really really easy to make std::span dangle: #include <cassert>
#include <span>
#include <utility>
#include <vector>
std::vector<int> append(std::vector<int>&& items, std::span<int> suffix) {
items.insert(items.end(), suffix.begin(), suffix.end());
return items;
}
auto make_appender(std::span<int> suffix) {
return [=](std::vector<int>&& items) {
return append(std::move(items), suffix);
};
}
auto make_appender34() {
std::vector<int> vec = {3, 4};
return make_appender(vec);
}
int main() {
auto append34 = make_appender34();
assert((std::vector<int>{1, 2, 3, 4} == append34({1, 2})));
}
|
|
The point we should take away is that is actually hard to invent plausible examples of the failure that we are being told Rust would prevent.