Questions tagged [const-reference]

131 questions
1
vote
1 answer

Why can't the .size() of an array passed to a function as a constant reference be used as a template parameter?

I can't seem to figure out why the following code doesn't work: #include template void a() {} template void b(const std::array& arr) { a(); // error: no matching function…
acumandr
  • 21
  • 4
1
vote
2 answers

Question about const member functions in c++

Can anyone explain me this error? Here is the code: class O{ unsigned x; unsigned y; public: O(unsigned x_ ,unsigned y_): x(x_), y(y_){ }; O& operator+= ( O & object){ x += object.x; …
Peanojr
  • 185
  • 1
  • 8
1
vote
3 answers

Why GCC refuses a const reference within a copy-assignment operation?

I want to overload a common copy-assignment operator normally. At first I used a interface that only requires a const reference to the source, and explicitly disabled the interface that accepts a modifiable reference, but I can not pass the…
Leon
  • 1,489
  • 1
  • 12
  • 31
1
vote
4 answers

A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

In C++, can the value of a const & change? Well, of course it cannot change, can it? That's what const means. Moreover, listen to Stroustrup: A const lvalue reference refers to a constant, which is immutable from the point of view of the user of…
thb
  • 13,796
  • 3
  • 40
  • 68
1
vote
1 answer

lvalue and rvalue as function parameters

I'm trying to understand Lvalue and Rvalue in C ++. So I'm using them as parameters passed to the functions. In this first case I have two functions, the first has a reference to an const int, in this case thanks to "const" (see link) I can pass to…
1
vote
1 answer

How can I make a Class with reference data member constructible with no arguments?

I have a class, say C, where one of the member data, say X, depends on user input. The user input may differ on every run, and in my current design, all instances of my classes stores a reference to the same object X. How can I tweak the design so…
1
vote
3 answers

Conditional assignment for const reference objects in C++

Here is a code snippet that illustrates my problem : class A {...}; const A& foo1() {...} const A& foo2() {...} void foo3(int score) { if (score > 5) const A &reward = foo1(); else const A &reward = foo2(); ... // The 'reward'…
Swaroop
  • 1,219
  • 3
  • 16
  • 32
1
vote
3 answers

How to define move assignment operator for const ref member of template class

I have the following template class, where the member is const ref type. Copying of object is disabled and wanted to only have move cntor and move assignment operator. Q1: How to implement move assignment operator for const ref type properly(Is it…
Const
  • 1,306
  • 1
  • 10
  • 26
1
vote
1 answer

List using with references, changes behavior when used as a member

Experimenting with this question/answer https://stackoverflow.com/a/50649120/225186 I produced what seems to be a legal recursive self referential class that implements a circular list: struct node{ int val; node const& next; }; int…
alfC
  • 14,261
  • 4
  • 67
  • 118
1
vote
1 answer

Reference for rvalue or not

I wonder will next code work correct with v and v2 variable or these are references for temporary variables? In other words, can I capture returned rvalue by reference? I think no, but my teamlead think another way. #include struct Foo…
voltento
  • 833
  • 10
  • 26
1
vote
0 answers

Optimize InputIterator dereference without making a copy if possible?

I have a legacy code in which the interface is defined for pointer only and I am trying to adapt some functions to take iterators. In the answers to this question Address of a dereferenced InputIterator? The case of istream_iterator it was noted…
alfC
  • 14,261
  • 4
  • 67
  • 118
1
vote
1 answer

Return by const reference in c++ where the receiver function copy the value is it worth it?

i have this situation which i wander if returning by const reference dose relay save something , this function may be called hundreds of times . i have : General Container that returns int as const reference struct Val { public: Val(int& v) …
user63898
  • 29,839
  • 85
  • 272
  • 514
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
1 answer

reference-to-cont class member initialized with non-const value

I have a class that should, as its input data, either use a reference to external data (without copying), or create the data itself based on other input. I prefer using references (to avoid dereferencing, since the data are matrices) and ended up…
Michal Kaut
  • 1,421
  • 1
  • 14
  • 25
1
vote
1 answer

How to get alternative value from function that gives wanted data via non-const output parameter for assigning reference-to-const variable to it?

The commented code works, but it is not a reference, so it has more computational cost. void CClass::Function(const CArray* ItemsInput) const { /* CArray Items; if (ItemsInput != nullptr) …
sergiol
  • 4,122
  • 4
  • 47
  • 81
1 2 3
8 9