V
V
Victor2016-01-07 14:07:44
Programming
Victor, 2016-01-07 14:07:44

Does it make sense to pass STL container by reference to function parameters to save memory and increase speed?

Or do modern compilers (primarily gcc) optimize this moment themselves?
The question arose after reading this article www.habrahabr.ru/post/117193, paragraph: Transferring everything and everywhere by pointers (links)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Armenian Radio, 2016-01-07
@gbg

Certainly has. How should the compiler know, maybe you really want to work with a copy, and not with the original.
Another matter, return from function. RVO (return value optimization) works there, so you can not worry, but simply give the container.

O
Oleg Tsilyurik, 2016-01-07
@Olej

Or do modern compilers (primarily gcc) optimize this moment themselves?

The compiler has no right to do this, because it will not be an optimization, but a sabotage.

P
pavelkolodin, 2016-01-07
@pavelkolodin

It doesn't "make sense", but the first rule. But the compiler is forbidden to optimize this, the semantics of the code changes from this, and not only optimization is done.

J
jackroll, 2016-01-07
@jackroll

const container_type &container - if the function does not change the state of the container.
container_type &container - if it changes.
Read The C++ Programming Language by Stanley Lippman.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question