Git Product home page Git Product logo

Comments (6)

aminroosta avatar aminroosta commented on June 2, 2024 1

@Killili I also removed the extensions folder, since the type system is not working and those are extra files.
Sorry about that :-)

from sqlite_modern_cpp.

bjoernpollex avatar bjoernpollex commented on June 2, 2024

This seems to be the exact use-case for which reference-wrappers were designed. Would you consider that an option?

Basically, your all function would look like this:

std::vector<Route> all(sqlite::database db) {
  route_builder.results.empty();
  db << "SELECT * FROM routes;"
    >> std::ref(route_builder);
  return route_builder.results;
};

Note that route_builder is wrapped in a call to std::ref.

from sqlite_modern_cpp.

faultyserver avatar faultyserver commented on June 2, 2024

From the example in the docs on std::ref, it looks like std::ref is primarily intended for creating reference types to be passed to functions which take reference parameters (That's a terrible explanation. Hopefully the example they give makes it a little more clear). In this case, however, I'm trying to pass a reference to a function that takes a value, so I don't think std::ref will work as intended.

When I tried using std::ref as you showed above, I got this error:

include/modern_sqlite/utility/function_traits.h:12:13:
error: reference to overloaded function could not be resolved; did you mean to call it?
    decltype(&Function::operator())
             ^~~~~~~~~~~~~~~~~~~~~

which confuses me, because there are no obvious (to me) overloads of operator() anywhere in my code, or in std::reference_wrapper. Note that without std::ref, the code compiles/works, the only issue being the copy semantics invoked by the pass-by-value function signature.

That said, is there any advantage to using a pass-by-value signature and not implementing the pass-by-reference method? My thoughts on the matter:

  • Passing by value doesn't readily allow using side-effects of the passed functions (the issue I originally ran into with the empty result vector).
  • With C++11, rvalues (like temporary lambdas) can be passed "by reference" using the rvalue reference syntax - T&& - as I showed above.
  • Using references avoids invoking unnecessary copy semantics. This is pretty minimal for simple types like lambdas. But with complex custom types (like my actual Builder class), invoking the copy constructor can be quite costly.

from sqlite_modern_cpp.

Killili avatar Killili commented on June 2, 2024

I see the point you are trying to make, and its a valid point.
But in this case isn't it a failure of the builder class for not having a working copy constructor? Or not being a proper Singleton? Even adding a copy constructor that just returns the object to be copied would solve this without the C++ Magic needed to work around it.

I'm trying to come up with better example where we would need that additional template but all i can think of would be solved by adding proper code or trivial in terms of performance loss.

Or and that's very possible i totally miss the point of this ;)

from sqlite_modern_cpp.

aminroosta avatar aminroosta commented on June 2, 2024

@faultyserver Thanks for the detailed explanation.

@bjoernpollex @Killili
I think faultyserver has a good point, we should support lvalue functors as well.
I did implement it in #61 vai Universal References, Let me know if you think something is missing.
If it looks good to you i will merge it tomorrow. (I think github recently added the git-apporve feature, i'm not sure if it is visible to you or not).

from sqlite_modern_cpp.

faultyserver avatar faultyserver commented on June 2, 2024

@aminroosta Wonderful! Your implementation certainly looks cleaner than mine. I was unaware of Universal References.

@Killili I think you were mostly on point, and a proper Singleton definitely would've solved the immediate issue as well. Thanks for the suggestion.

PS: Thanks for this line.

return std::move(person_builder.results); // move to avoid copying data ;-)

from sqlite_modern_cpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.