Questions tagged [pass-by-const-reference]

52 questions
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
1 answer

Why can't we pass const values by reference to inout functions in swift?

In C, although we declare a value as const int a = 5;, we can pass &a to a function declared as void someFun(const int *);. As a rule of thumb, in C, when the original value is need not to be changed, i) if the size of object is less than or equal…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
1
vote
2 answers

Why does string_view::operator== accepts parameters by value

I was reading source code of string_view, and found that operator== accepts parameters by value. template constexpr bool operator==(basic_string_view<_CharT, _Traits> __x, …
Jouni
  • 321
  • 1
  • 8
1
vote
2 answers

Should templated math functions take values or const references?

Suppose I want to implement some simple mathematical function; for example suppose it's a reimplementation of (C++17's) std::clamp: This function takes a number, a lower bound and an upper bound, and sets the number to one of those bounds if its out…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
0 answers

constant reference parameter gives warning when passing a temporary

The following code gives me a warning about passing a temporary as a parameter to a function which takes a reference: struct TempObject { typedef TempObject& reference; const int First, Second; TempObject(int first, int second) :…
bfair
  • 1,101
  • 7
  • 16
1
vote
1 answer

If a variable is passed as a constant does it get passed by reference?

If I pass a variable as a constant does it automatically get passed by reference? procedure foo(const x : integer) I can already pass a variable by reference like this: procedure foo(var y : integer); Ideally I'd want something like the code…
J_Strauton
  • 2,270
  • 3
  • 28
  • 70
1
vote
3 answers

How to modify/update the internal state of an object passed by const reference

Passing an object by const reference means we can't modify any of it's members. But suppose the object contains a vector member. How do we write const methods to read the contents of the vector member assuming that a…
mihai
  • 4,592
  • 3
  • 29
  • 42
0
votes
2 answers

Weird behavior with std::string reference class member

Given this code: #include class Foo { public: Foo(const std::string& label) : label_(label) {} void print() { std::cout << label_; } private: const std::string& label_; }; int main()…
0
votes
1 answer

Is relying on the const-ness of arguments bind to const& parameters a recipe for thread un-safety?

I was just saw code like this /* whatever */ foo(std::string const& s) { // stuff auto L = s.length(); int i{/* init based on L */}; while (i < L) { // do other stuff and maybe ++i; } } and I was going to suggest that one can…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
3 answers

Multiple functions with the same name but their parameters are either constant or received by value or by reference

The title is a bit lengthy, but it's best explained by an example: Suppose we have the following functions in C++: void SomeFunction(int num) { //1 } void SomeFunction(int& num) { //2 } void SomeFunction(const int& num) { //3 } void…
0
votes
0 answers

Are temporaries const? Pass by const reference

#include using namespace std; void f(const int& x) { } int g() { int x = 0; return x; } int main() { f(g()); } If i remove the const from f it doesnt work; What does this mean, is the temporary returned from g a const???
0
votes
1 answer

Using find_if with a vector of pointers: How to pass pointer by const reference to lambda?

In the following code I try to compare a vector of pointers via find_if and determine which contains a member a == 5 (in this case both of course, but it shows my case). However it doesn't compile. #include class obj { public: …
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

C++ const reference template function argument type is itself a reference, why?

I have the following function: template void f(const T& val) { using value_type = T; using sub_type = typename value_type::sub_type; //etc... } However, I am running into the problem that the compiler is telling me that T is…
111111
  • 15,686
  • 6
  • 47
  • 62
0
votes
1 answer

Can we have a setter member function as const referenced from a const object?

I have a class like this : class XYZ { public : bool var1 = true; ABC *var2 = nullptr; public : XYZ() = default; void SetVar1(bool flag) { var1 = flag; } void SetVar2(ABC *abc) { var2 = abc; } } I want to know that if my…
0
votes
1 answer

When is the address of a const reference function parameter unique?

In my example code below, I'd like to know when two calls to log_cref_address will reliably print the same address. #include #include #include using namespace std; void log_cref_address(const int& t) { cout <<…