sqlite_orm
sqlite_orm copied to clipboard
Add non-const getters to iterator
Iterator has const operators since #685 (see here).
This makes it impossible to do in-place update (modify the value and pass it to update
) when iterating the database.
I think we should add non-const overloads.
Thanks!
hi. Can you provide a small code snippet to repro it please?
Sure ! Here you go:
struct S {
int id;
int value;
int some_other_information;
};
void f() {
auto db = make_storage(path,
make_table("currency",
make_column("id", &S::id, primary_key()),
make_column("value", &S::code),
make_column("some_other_information", &S::some_other_information)));
for (auto& s : db.iterate<S>()) {
// get a new value here for the id, so we want to update it in database
int new_value = 42;
// easy way (not working, s is actually a const&)
s.value = new_value;
db.update(s);
// longer way (working)
db.update(S{s.id, new_value, s.some_other_information});
}
}
@kiwixz BTW you can use const_cast
which will make the work. Why don't you use this approach?
I would not want to do that, if I'm allowed to modify the value then it should not be returned to me as a const&. const_cast
says "I dont care if I'm not supposed to, I will modify this anyway".
PR https://github.com/fnc12/sqlite_orm/pull/1229
merged. Please check dev
branch. Also it will be merged into master
on the next release
Works great, thanks!