Questions tagged [const-reference]

131 questions
2
votes
1 answer

Objects seemingly not being passed by reference in C++

Edit: The problem has been found to be that the Wrapper class is creating copies of the BSCallFunction when passed into the MCEngine function and in the SimulationEngine class shown below. With this in mind I still need to figure out how to make the…
Oscar
  • 279
  • 1
  • 10
2
votes
1 answer

friend functions with const parameters

I got to know that to make a friend function, friend function should be explicitly declared in enclosing scope or take an argument of its class. However, this one seems to be a caveat, I am failed to understand. Why does the call to f1(99) not…
Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45
2
votes
2 answers

How do I pass constexpr values to CUDA device-side functions taking const references?

Consider the following code: template __host__ __device__ int foo1(const T& x); template __host__ __device__ int foo2(T x); These two functions correspond to two common ways to pass "in"-parameters rather than "out" or…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
0 answers

Why const references suppress automatic conversion in C++

In the book "Introduction to Design Patterns in C++ with Qt" I found "It is still slightly faster to pass by const reference, which enables C++ to optimize out the copy operation entirely. With const reference, the function cannot make changes to…
brian
  • 265
  • 1
  • 9
2
votes
2 answers

Function with parameter type that has a copy-constructor with non-const ref chosen?

Some time ago I was confused by the following behavior of some code when I wanted to write a is_callable trait. Overload resolution won't call functions accepting arguments by non-const ref, right? Why doesn't it reject in the following…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
2
votes
2 answers

C++ return by reference and return by const-reference value is copied

I have some questions about returing a reference of a class member variable. I have the following code: #include #include #include #include void PrintSet (const std::string & str, const std::set &…
2
votes
0 answers

Can a Second Const Reference Extend the Lifetime of a Temporary

Background: I noticed that the signature of std::max is: template const T& max(const T&, const T&); and I wondered about the implications of returning a reference to a const T... If we pass in two L-values, it makes sense we could…
user137364
  • 305
  • 1
  • 7
2
votes
2 answers

Allowing both pre-computed and computed-on-the-fly results

Consider: template struct C { std::vector f(const T &t) const { return t.f(); } }; T::f must compute the required vector. However, some T's pre-compute the vector and we would like to avoid creating a copy in such…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
2
votes
2 answers

Storing return value as a const reference versus as a value

Suppose that a function has the signature std::string GetString(); and that it returns a new string by value. Now given the following code: // (a) Store the return value to a const reference. const std::string& my_string = GetString(); // (b)…
Jae Heon Lee
  • 1,101
  • 6
  • 10
2
votes
1 answer

Aliasing a variable using const reference

When dealing with T instances obtained from an array or trough any other longish syntax, I often use a const T& to alias the object and make my code more readable (of course only if the lifetime of the object allows it). I've seen this elsewhere,…
Daerst
  • 954
  • 7
  • 24
2
votes
1 answer

Why reference can not capture temporary while const ref and rval ref can

Why reference can not capture temporary value while const reference and rvalue reference can capture and prolong object life. In other words while two first lines are legal but third not: const string &a = string("a"); string &&b =…
Trismegistos
  • 3,821
  • 2
  • 24
  • 41
2
votes
2 answers

What is the type of a const reference?

I know that passing a const reference variable to a function’s const reference parameter does not cause the function parameter to be of the type "const reference of a const reference of the referee's type". The variable name of the const reference…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
1
vote
1 answer

Why is the compiler rejecting an explicit specialization for a const pointer?

Consider the following program: template T foo(const T& x); template <> char const * foo(char const *& x); where I'm just using specializing const char*. The compiler won't accept this! It tells me…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
1 answer

(C++) Using multiple operator overloads with const reference parameters

I have been working on a matrix class and I have recently learnt about passing const references to operator overloads so that I can have multiple of them on the same line. The problem I encountered is when defining a function for an operator…
1
vote
2 answers

Does passing by const reference really save the memory cost when it has to convert the type?

There are many existing questions about const reference (reference to const). But when there is an implicit conversion, const reference also causes a new address. I wrote this example bellow: int i = 42; double d = 4.2; const int& ri1 = i;…
ripfreeworld
  • 143
  • 1
  • 13
1 2 3
8 9