Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is better, auto&&, auto or auto const [duplicate]

Tags:

c++

c++14

If I have a type like this

std::vector<int> const value = ...

Which one is a better solution?

style 1 :

for(auto v : value){
  //do something
}

style 2 :

for(auto &&v : value){
  //do something
}

style 3 :

for(auto const v : value){
  //do something
}

All of them keep the constness of the type.

Style 2 is the most generic solution.

According to what I know, for primitive type like int, double etc, pass by value is prefer over pass by const reference, so I think style 1 and style 3 is better than style 2 if we know the type of vector is primitive type. Please forgive me if this question sound stupid.

like image 531
StereoMatching Avatar asked Oct 19 '25 15:10

StereoMatching


1 Answers

It really depends on your needs and what you're iterating over.

Styles 1 and 3 make a copy of the element. This means you can't change the original container. For small types it may be beneficial in terms of performance (benchmark yourself on your target architecture), but usually is just a bad default.

You then have to decide whether you're one of the const by default folks. If yes, take style 3.


Style 2 is indeed most generic, but not necessarily in the good way. Unless you want to further move from the element, there's little point to taking a non-const reference to a sequence generated on the spot, i.e.:

for(auto&& x : std::vector<int>{1,2,3})
{
    // x = 42; ???
}

If you look at C++ Core Guidelines here and here, you'll see no examples with auto&& and they suggest taking const& by default unless you're going to modify the variable. That's what I'd stick to.

like image 86
krzaq Avatar answered Oct 22 '25 05:10

krzaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!