0

im pretty new to C++, learning by doing right now. Within the class material, I have the following function:

std::vector<std::complex<double>> material::get_idx(const std::vector<double>& numbers) {

  std::vector<std::complex<double>> res;

  for (auto& num : numbers) {
    // calculate with num (we don't modify it) and push_back to res 
    ...
  }

  return res;
}

A couple of minutes ago I used the normal for loop like this: for(size_t i=0; i<numbers.size(); i++). Since I've read that using ranged-based for loop with auto can be more efficient than plain for loops, I changed it. Now I'm asking myself if it's correct/senseful/good practice to write the ranged-based for loop with a reference like this: for(auto& num : numbers) and what's the difference to using for(auto num : numbers). I'm kind of confused because I'm already handing over the numbers vector per reference in the signature of the get_idx function. Of course I want to hand over the vector numbers to the function get_idx per reference, but how about looping over those elements via reference?

19
  • 3
    My advice, const auto& if you don't want to modify, auto&& if you do. Commented Mar 11, 2021 at 13:20
  • Hey @NathanOliver, thanks for your answer, what is the difference of using const auto& instead of just const auto? Commented Mar 11, 2021 at 13:21
  • The first is a reference, so no copy. The second is a value so a copy is made. Copying can be expensive. Commented Mar 11, 2021 at 13:22
  • 1
    you never run out of new things to learn in C++, though i am not always sure if thats a good thing ;) Commented Mar 11, 2021 at 13:49
  • 1
    Well at least it's a good thing for sure to have people here being able to explain things voluntarily. Thanks again, have a great day folks! Commented Mar 11, 2021 at 13:51

1 Answer 1

1

auto makes a copy, auto& makes an lvalue reference of any kind but will not bind to rvalues, auto const& makes an lvalue reference to const that can bind to rvalues and induce reference lifetime extension, auto&& generates a forwarding reference that can bind to anything and can induce reference lifetime extension. auto const makes a copy and makes it immutable.

Which is more correct depends on which you want.

You could just type double const&, double const or double instead of anything to do with auto.

I start with auto&&, which is shorthand for "I do not care, just make it work". Next I'd use auto, as values make reasoning easier. And if I had a worry about making it clear I am not editing, auto const or auto const&.

If I wanted to edit the elements inplace, auto&.

Sign up to request clarification or add additional context in comments.

1 Comment

Its for(double const& num : numbers) rather than for(std::complex<double> const& num : numbers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.