Giter VIP home page Giter VIP logo

Comments (6)

beinhaerter avatar beinhaerter commented on July 24, 2024 1

Hi @beinhaerter, may I ask you double-check the snippet, please? I think that the lvalue in the last std::cout should be rvalue.

Thanks for pointing that out. I corrected the code snippet.

from cppcoreguidelines.

siebenschlaefer avatar siebenschlaefer commented on July 24, 2024 1

@iglesias

[...] I'd follow the approach in your first bullet point [...]

Taking the argument by reference to const and then creating a copy is less efficient
if the caller passes an rvalue to the function
because that creates copy, whereas taking the argument by value would move.

from cppcoreguidelines.

iglesias avatar iglesias commented on July 24, 2024

Hi, I can't sure what you are missing or misunderstanding, although your explanations dealing with rvalue (reference(s)) are not clear to me. This also pertains your third and fourth questions.

I don't think your use case is so exotic and to comply with the guideline as it is I'd follow the approach in your first bullet point: I think that, if I am understanding you correctly,

auto f(T const& t) {
  // This would be what you called little bit more code, correct? Strictly, with the const& too.
  auto t_copy = t;
  ...
}

is not in fact "tiny bit more"(?) complicated code than

auto f(T t) {
  ...
}

from cppcoreguidelines.

beinhaerter avatar beinhaerter commented on July 24, 2024

@iglesias siebenschlaefer said that the const& code is less efficient when you pass an rvalue to it. You can compare it using the code below.

I agree with @siebenschlaefer and that is why I am using this pass-by-value approach in my code, too. Contradicting 'F.18: For “will-move-from” parameters, pass by X&& and std::move the parameter' I even use that for will-move-from parameters to give the caller of my function more freedom and to not require him to std::move().

#include <iostream>

struct S {
    S() { std::cout << "ctor\n"; }
    S(const S&) { std::cout << "copy ctor\n"; }
    S(S&&) { std::cout << "move ctor\n"; }
};

S get() noexcept { return S{}; }

void fc(const S& s) { S copy{s}; }

void fr(S&& s) {}

void fv(S s) {}

int main() {
    S s;
    std::cout << "\nf taking const&, getting lvalue\n";
    fc(s);
    std::cout << "\nf taking const&, getting rvalue\n";
    fc(get());
    std::cout << "\n\nf taking rvalue\n";
    fr(get());
    std::cout << "\n\nf taking value, getting lvalue\n";
    fv(s);
    std::cout << "\nf taking value, getting rvalue\n";
    fv(get());
}

from cppcoreguidelines.

iglesias avatar iglesias commented on July 24, 2024

Hi @beinhaerter, may I ask you double-check the snippet, please? I think that the lvalue in the last std::cout should be rvalue.

Disregarding that and assuming the snippet's point is correct. I feel that better measurement is needed to reach the conclusion that it is less efficient.

from cppcoreguidelines.

hsutter avatar hsutter commented on July 24, 2024

Editors call: Thanks! We intend to update the wording to cover this case better.

from cppcoreguidelines.

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.