Questions tagged [const-reference]
131 questions
198
votes
6 answers
Does a const reference class member prolong the life of a temporary?
Why does this:
#include
#include
using namespace std;
class Sandbox
{
public:
Sandbox(const string& n) : member(n) {}
const string& member;
};
int main()
{
Sandbox sandbox(string("four"));
cout << "The answer…

Kyle
- 4,487
- 3
- 29
- 45
87
votes
5 answers
C++ Return value, reference, const reference
Can you explain to me the difference between returning value, reference to value, and const reference to value?
Value:
Vector2D operator += (const Vector2D& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
Not-const…

Majak
- 1,543
- 1
- 14
- 28
61
votes
2 answers
Why is `const T&` not sure to be const?
template
void f(T a, const T& b)
{
++a; // ok
++b; // also ok!
}
template
void g(T n)
{
f(n, n);
}
int main()
{
int n{};
g(n);
}
Please note: b is of const T& and ++b is ok!
Why is const T& not…

xmllmx
- 39,765
- 26
- 162
- 323
45
votes
4 answers
Returning const reference to local variable from a function
I have some questions on returning a reference to a local variable from a function:
class A {
public:
A(int xx)
: x(xx)
{
printf("A::A()\n");
}
};
const A& getA1()
{
A a(5);
return a;
}
A& getA2()
{
A a(5);
…

aJ.
- 34,624
- 22
- 86
- 128
44
votes
4 answers
Avoid exponential grow of const references and rvalue references in constructor
I am coding some templated classes for a machine learning library, and I'm facing this issue a lot of times. I'm using mostly the policy pattern, where classes receive as template argument policies for different functionalities, for…

Federico Allocati
- 565
- 4
- 9
33
votes
3 answers
const-ref when sending signals in Qt
This is a thing that I never quite got with const-ref and I really hope that someone could explain it to me.
When calling a function inside of another function, I get that const-ref is the best way when passing stack objects that I don't plan to…

chikuba
- 4,229
- 6
- 43
- 75
31
votes
5 answers
C++: Is it better to pass an enum as a value or as a const reference?
There are sort of two related questions here:
A) How is enum implemented? For example, if I have the code:
enum myType
{
TYPE_1,
TYPE_2
};
What is actually happening? I know that you can treat TYPE_1 and TYPE_2 as ints, but are they…

Casey Patton
- 4,021
- 9
- 41
- 54
24
votes
2 answers
const reference to temporary vs. return value optimization
I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization.
LargeObject…

Robert Rüger
- 851
- 9
- 21
23
votes
2 answers
Const reference qualifier on a member function
I have seen in an anwser there:
Is returning by rvalue reference more efficient?
The member function definition:
Beta_ab const& getAB() const& { return ab; }
I am familiar with the cv-qualifier (const) on member functions, but not const&.
What does…

galinette
- 8,896
- 2
- 36
- 87
21
votes
4 answers
In c++, why does the compiler choose the non-const function when the const would work also?
For example, suppose I have a class:
class Foo
{
public:
std::string& Name()
{
m_maybe_modified = true;
return m_name;
}
const std::string& Name() const
{
return m_name;
}
protected:
std::string…

Caleb Huitt - cjhuitt
- 14,785
- 3
- 42
- 49
19
votes
2 answers
Const reference to a casted int from unsigned int
I am having some trouble understanding the behaviour in this snippet:
unsigned int i = 2;
const int &r = i;
std::cout << r << "\n";
i = 100;
std::cout << r << "\n";
The first print statement gives 2 as I expect, but when I change the value of the…

KKOrange
- 590
- 4
- 14
15
votes
2 answers
What is this strange copy constructor error complaining about?
I'm on Visual Studio 2017. Recently, because I didn't like the non-conforming standards to C++ I went ahead and disabled the non-standard language extensions in the options. So far so good. Now I have a problem.
#include
#include…

Zebrafish
- 11,682
- 3
- 43
- 119
15
votes
4 answers
Returning const reference of an arraylist
I really admire java features and I don't want to give up using it for the next problem:
I have a class that might be inherited, and inside of it is a private ArrayList arr; So the setter function is ok , but the getter function return arr; returns…

Ismail Marmoush
- 13,140
- 25
- 80
- 114
12
votes
3 answers
Inline Function Arguments Passing
Is there a need performance-wise for inline functions to pass its arguments by const reference like
foo(const T & a, const T &b)
compared to by value
foo(T a, T b)
if I don't change the values of a and b in the function? Does C++11 change…

Nordlöw
- 11,838
- 10
- 52
- 99
12
votes
1 answer
Binding temporary to const reference in c'tor initializer list
Section 12.2.5 in C++03 says "A temporary bound to a reference member in a
constructor’s ctor-initializer (12.6.2) persists until the constructor exits"
So I tried following program
#include
using namespace std;
struct foo
{
foo()
…

Happy Mittal
- 3,667
- 12
- 44
- 60