GithubHelp home page GithubHelp logo

Comments (6)

pbalcer avatar pbalcer commented on July 20, 2024

This is modeled after p<> where a conversion operator for a non-const reference would require a snapshot. Such approach would probably lead to many unnecessary snapshots, that's why there are explicit getters.

For consistency sake, v<> behaves the same, and to get a mutable reference, you have to call get(). But... I guess returning a reference would make more sense in this case, since there's no snapshot.

Can we have both though? How would the compiler choose between a conversion to T and T&?

from libpmemobj-cpp.

tomaszkapela avatar tomaszkapela commented on July 20, 2024

I don't think you can have both. The compiler cannot do overload resolution for you based on the return type and that is what you are suggesting, right? I'm not up to date on the topic, so sorry if I'm far off with my comment.

There is syntax where the compiler can behave differently for rvalues, but that is not the case here, right?

from libpmemobj-cpp.

vinser52 avatar vinser52 commented on July 20, 2024

Return type is not part of function signature. But if you would like to have both we can have const and non-const versions of the conversion operator:

`operator T &() noexcept
{
return this->get();
}

operator const T&() const noexcept
{
return this->get();
}`

Note, that in my example even const version does not return by value. I am using const reference. It helps to avoid unnecessary copy. Furthermore, type T might have no copy-constructor.

from libpmemobj-cpp.

igchor avatar igchor commented on July 20, 2024

I think in case of conversion to T& it will be ok to have just one non-const conversion operator. After all, T& can be used in context where const T& is required, right?

from libpmemobj-cpp.

vinser52 avatar vinser52 commented on July 20, 2024

@igchor I agree with you. This changes already part of #40

from libpmemobj-cpp.

igchor avatar igchor commented on July 20, 2024

We have already merged this change to master. We also fixed some other issues with v which we found while testing operator T& so you should probably rebase.

from libpmemobj-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.