Pass an object to a function without wrapping it in std::ref, while the parameter is specified as a const reference


Dan Clemenko

I have the following C++ code (just an example of a simple problem)

#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 the parameter of the function get_firstis a const reference,

And I'm passing the whole vector some_vecinstead of wrapping it into std::ref. Will c++ copy the complete object here?

Michael Chodakis

std::refNot for that. It is used to convert an existing reference to an object and is used where a reference cannot be accepted, e.g. std::vector<>in . Most likely it internally converts the wrapped reference to a pointer.

In your example, the parameter is automatically passed as a reference.

Related


Pass std::function as parameter to const void *

Aria_JC I have the following function declaration: void get_data(struct myStruct* const value, const void * const data); I have another function where I want to add a std::functionas a parameter: // I think the std::function definition is where my problem is

std::transform() pass reference in function parameter

Roy My question/concern is about the function parameter to be used in std::transform(). In the following code, if I use "pass by reference" in the integer parameter of ithe square function (ie int squared(int i)), it doesn't compile). I had to change it to pas

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass function object to std algorithm by reference

Tomilov Anatoliy Wouldn't it be better to pass functional objects into STL algorithms by forwarding references rather than by value? It will allow to take advantage of the reference qualifier of operator ()the passed function object . There are several std::fo

Pass function object to std algorithm by reference

Tomilov Anatoliy Wouldn't it be better to pass functional objects into STL algorithms by forwarding references rather than by value? It will allow to take advantage of the reference qualifier of operator ()the passed function object . There are several std::fo

Pass function object to std algorithm by reference

Tomilov Anatoliy Wouldn't it be better to pass functional objects into STL algorithms by forwarding references rather than by value? It will allow to take advantage of the reference qualifier of operator ()the passed function object . There are several std::fo

Pass function object to std algorithm by reference

Tomilov Anatoliy Wouldn't it be better to pass functional objects into STL algorithms by forwarding references rather than by value? It will allow to take advantage of the reference qualifier of operator ()the passed function object . There are several std::fo

Pass function object to std algorithm by reference

Tomilov Anatoliy Wouldn't it be better to pass functional objects into STL algorithms by forwarding references rather than by value? It will allow to take advantage of the reference qualifier of operator ()the passed function object . There are several std::fo

Pass pointer to const ref function

Dean Why does the following work? #include <iostream> using namespace std; class PolyLine { public: PolyLine() = default; PolyLine(PolyLine * ptr) { std::cout << "Ctor called" << std::endl; } }; void function(const PolyLine& pt) { } int main() {

Pass const char instead of std::string as function parameter

Abruzzo Forte and Gentile This is a newbie question, but I don't understand how it works. Suppose I have the following function void foo(const std::string& v) { cout << v << endl; } and the call in my program below. foo("hi!"); Essentially, I'm passing a