Questions tagged [pass-by-const-reference]
52 questions
4
votes
1 answer
Which is faster? Pass by reference vs pass by value C++
I thought that pass by reference should be faster then pass by value because the computer isn't copying data, it just points to the address of data.
But, consider the following C++ code:
#include
#include
#include
using…

off99555
- 3,797
- 3
- 37
- 49
4
votes
3 answers
What's the "correct" way to pass an empty vector to an object?
I am working on a fairly large C++ project which unfortunately doesn't really use C++ to its full potential. Large portions of the code are still plain C wrapped in ridiculous C++ classes.
So I tried to make the code more readable and more safe by…

Excelcius
- 1,680
- 1
- 14
- 31
3
votes
1 answer
Why does C++ give preference to rvalue reference over const reference while function call?
So I wrote a code in C++ 11
#include
using namespace std;
void print (int &&a)
{
cout<<"rval ref";
}
void print (const int& a)
{
cout<<"const ref";
}
int main()
{
print(9);
}
The output of code was fascinating that it…

simplekind
- 39
- 4
3
votes
4 answers
c++ copy construct parameter passed by value
I want freeFunct to do non const stuff on its own copy of object a.
Let's say that freeFunct is required to be a free function
because in real code cases it takes many different parameters,
calls several public functions from all of them and there…

jimifiki
- 5,377
- 2
- 34
- 60
2
votes
2 answers
Why does the "in" keyword allow mutation of properties C#?
I am currently working on a function where an object (of self written type Catalog) is passed and shall not be mutated / changed in the called function. Here a quick example.
public override bool Migrate(in Catalog fromCatalog)
{
…

Patrick Cerny
- 191
- 2
- 8
2
votes
1 answer
C++20 : Memory allocation of literal initialization of const references
I am trying to optimize for speed of execution a piece of code using the factory design pattern.
The factory will produce many objects of a class having some members that are constant throughtout the execution of the program, and some members that…

LastStarDust
- 55
- 7
2
votes
2 answers
Pass-by-value and std::move vs forwarding reference
I encounter the pass by value and move idiom quite often:
struct Test
{
Test(std::string str_) : str{std::move(str_)} {}
std::string str;
};
But it seems to me that passing by either const reference or rvalue reference can save a copy in…

Alex O
- 1,429
- 2
- 13
- 20
2
votes
1 answer
Reference at std::thread parameters
I have two functions
void f(const int &x) {}
void g(int& x) {}
I can make
int x = 0;
std::thread t1(f, x);
But I can't create std::thread t2(g, x), in this case i need make std::ref(x) instead of just x, why is it necessary?
And why it possible to…

Артём Гаркавый
- 383
- 2
- 10
2
votes
2 answers
error: ambiguous overload for 'operator=' in swap function using the copy-and-swap idiom
While using the copy-and-swap idiom in a class that has constant references as members,
the above error occurs.
Example code:
#include
#include
using std::reference_wrapper;
class I_hold_reference;
void…

Dávid Tóth
- 2,788
- 1
- 21
- 46
2
votes
2 answers
Does an rvalue keep its "status" when a const reference parameter binds to it?
Let T be an arbitrary type. Consider a function that takes a const [lvalue] reference:
void f(const T &obj);
Suppose that this function internally makes a call to another function, which has an rvalue reference overload:
void g(T &&obj);
If we…

Anakhand
- 2,838
- 1
- 22
- 50
2
votes
1 answer
Are constant references still best practice in c++11 and later?
I recently read an article about the new move semantics in C++. It was about the confusion how to best implement a return value for a large object. The conclusion was, just implement it like return by copy and let the compiler decide if a move works…

Flovdis
- 2,945
- 26
- 49
2
votes
1 answer
C++ Immutable custom class pass by reference or value
I've made a custom class which involves a ton of number and string computation. I've made my class immutable by only providing accessors and no mutators. Once the object is constructed, there is no changing a single property of it.
My question from…

Hatefiend
- 3,416
- 6
- 33
- 74
2
votes
1 answer
How do I safely hold on to a C++ const reference?
aka. how do I prevent a const& parameter from accidentally binding to a temporary?
We have a class that essentially looks like this:
template
struct Observer {
const T* target;
void observe(const T& x) {
target = &x;
…

Martin Ba
- 37,187
- 33
- 183
- 337
2
votes
0 answers
Forbid rvalue binding via constructor to member const reference
I am working on a matrix view class, of which constructor takes a matrix as a parameter and binds it to a const reference member. I would very much like to avoid binding rvalues, since they don't bind via a constructor parameter, and we end up with…

vsoftco
- 55,410
- 12
- 139
- 252
1
vote
2 answers
C++ struct as function argument vs. multiple const ref parameters vs. C++ core guidelines vs. performance?
I'm currently trying to decide whether to "structify" a rather long parameter set:
void fooCopy1(std::string const& source, std::string const& destination, std::string const& filter, std::string const& temp);
to this:
struct FooCopyArgs {
…

Martin Ba
- 37,187
- 33
- 183
- 337