I have the following c++ code(just a simple example for a question)
#include <iostream>
#include <string>
#include <vector>
double get_first(const std::vector<double>& vec) {
return vec[0];
}
int main()
{
std::vector<double> some_vec = {1, 2};
std::cout << get_first(some_vec);
}
So here parameter of the function get_first
is const reference,
while I pass whole vector some_vec
instead of wrapping it into std::ref. Does c++ copies full object here?
std::ref
is not for that. It is used to convert an existing reference to an object, used where a reference cannot be accepted, like within a std::vector<>
. The idea is that references cannot be reassigned or set to null, so stuff in STL containers like moving etc wouldn't work with a raw reference. Most likely it internally converts the wrapped reference into a pointer.
In your example, the paramter is automatically passed as a reference.