Structured binding for fixed-size span

After finding out you can ergonomically convert std::vectors into fix-sized std::spans, I thought I’d try out structured bindings for std::vector: auto _ = std::vector{ 1,2,3 }; std::span<int, 3> a = std::span(_).first<3>(); auto [b,c,d] = a; But it’s not working https://godbolt.org/z/nhrYn65dW However, it seems from P1024 Usability Enhancements for std::span which was accepted that this should… Read More Structured binding for fixed-size span

Why can't std::unique_ptr be returned after structured binding without using std::move?

When I try to compile the following code I get the error C2280. I guess the compiler is trying to copy the unique_ptr or something. #include <memory> std::pair<int, std::unique_ptr<int>> CreatePair() { std::unique_ptr<int> my_int(new int); return { 1, std::move(my_int) }; } std::unique_ptr<int> GetUinquePtr() { auto [ignore, unique_ptr] = CreatePair(); return unique_ptr; // <- Build error C2280… Read More Why can't std::unique_ptr be returned after structured binding without using std::move?

decltype does not preserve ref qualifier from structured binding

Usually decltype perseveres the ref qualifiers auto a = 0; auto& a_ref = a; static_assert(std::is_reference_v<decltype(a_ref)>); But apparently not when it’s argument is obtained from structured binding auto p = std::pair{1, 2.f}; auto& [i, d] = p; static_assert(std::is_reference_v<decltype(i)>); // fails https://godbolt.org/z/qWT574fr9 I’m pretty sure that i and d are references here. They should be according to… Read More decltype does not preserve ref qualifier from structured binding

Structured bindings in foreach loop

Consider fallowing peace of code: using trading_day = std::pair<int, bool>; using fun_intersection = vector<pair<int, bool>>; double stock_trading_simulation(const fun_vals& day_value, fun_intersection& trade_days, int base_stock_amount = 1000) { int act_stock_amount = base_stock_amount; for(auto trade : trade_days) { if (trade.second == BUY)// how to change it to trade.action? { } else { } } } What I would… Read More Structured bindings in foreach loop