I used to pass every complex structure by const &
or at least by &
. But with the new std::move
semantic and all the optimizations that compilers offer today, is it still the option to go?
Consider such example:
struct Task{
unsigned timeMS;
void(*function)(unsigned, unsigned) = 0;
Task(unsigned timeMS, void(*function)(unsigned, unsigned))
: timeMS(timeMS), function(function){}
};
class Timeline{
std::vector<Task> tasks;
...
};
class App{
...
public:
inline void addTask1(const Task &task){ timeline.add(Task); }
inline void addTask2(Task &task){ timeline.add(Task); }
inline void addTask3(Task task){ timeline.add(Task); }
};
Which one of addTask1, addTask2, addTask3
is the way to go? Assume that App::addTask()
is an heavily used method.
I guess that const &
requires to create a copy, but I've learned that things are not as simple as they look. It's enough to mention RVO (http://en.wikipedia.org/wiki/Return_value_optimization) - and I'm sure that there are much more things that should be taken into account (and I'm not aware of them yet).
I know that inline
is in fact just the suggestion for compiler, rather then an order. But does it change anything in const &
vs &
vs by value
battle?
I am working with VC++ 2013, I'm not focused on gcc too much.
P.s. Note that App::addTask
call Timeline::add
which call vector::push_back
. So the parameter is passed more then once - should I make both App::addTask
and Timeline::add
of the same "type" (const &
vs &
vs by value
).