Questions tagged [const-reference]
131 questions
1
vote
2 answers
Constructor and const reference
I;m study C++ right now (started like 2 days ago) and I have some trouble with writing Copy C'tor of Node.
Node is a class as following:
template
class Node {
T* data;
Node* next;
friend class Iterator;
…

SiuusSE
- 13
- 3
1
vote
1 answer
const string reference as non-type template argument
I am trying to have a const string reference as non-type template argument , i am not able to get past this compilation error .
test.h :
#include
#include
template class TestTmplt
{
};
const…

N3Xg3N
- 97
- 1
- 12
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
Constant reference parameter causing unresolved external symbol
Below is a simplified version of some code I wrote. this code works fine so far
class.h
namespace myNamespace
{
class myClass
{
public:
myClass(unsigned width, unsigned height);
myClass(OtherClass& other, unsigned width,…

Stebly
- 13
- 6
1
vote
4 answers
Why does this work? Returning const references in C++
I am fooling around with C++ and const references and am confused why this code works:
#include
class A {
public:
A() : a_(50) {}
const int& getA() const { return a_; }
private:
const int a_;
};
int main(int argc, char*…

devillighter
- 43
- 3
1
vote
3 answers
Const references for exposing fields
I have a class Date. let date be:
class Date
{
private:
unsigned int _day;
unsigned int _month;
unsigned int _year;
public:
const unsigned int& Day;
const unsigned int& Month;
const unsigned int& Year;
Date() :…

Sellorio
- 1,806
- 1
- 16
- 32
0
votes
0 answers
What are the different ways to pass a parameter by reference and how do they differ?
Could anyone please explain all the different types of ways to pass by parameters? Specifically, involving ampersands ('&') and the 'const' keyword.
I've seen many kinds, but I don't really understand what they do, and when they should be used.
To…

ericssonl07
- 11
- 2
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
2 answers
Why const reference discards in implicit template instantiate?
I write a class with template method and use it like below.
#include
using namespace std;
class A {
public:
template void DoSomething(uint32_t index, T arg);
};
template<>
void A::DoSomething(uint32_t index,const string…

MohsenTi
- 361
- 1
- 2
- 17
0
votes
1 answer
Const references with and without type conversion - why feature type conversion at all? - C++
I began learning C++ this week, and currently I am reading about compound types and constant variables. Unlike in most cases, references to const support type conversion by creating a temporary variable. But if so, then what's the difference in…

Oleg Shevchenko
- 19
- 2
0
votes
0 answers
C++ const-reference loop over a vector containing map
#include
#include
#include
#include

Huy Le
- 1,439
- 4
- 19
0
votes
1 answer
c++ return rvalue as const left ref
Since we can pass rvalue to function taking const left ref,
void taking(const string& ref) {}
taking("abc");
can we return rvalue as const left ref without reporting warning?
const string& returning()
{
static string s = "abc";
if (1)
…

hczstev
- 41
- 4
0
votes
1 answer
Why is temporary object living after end of the expression
Why if
string getString(){
return string("string");
}
int main(){
const string& a = getString();
cout << a;
}
Will give an UB
This:
class vector{
void push_back(const T& value){
//...
new(arr + sz) T (value);
…

Max Popov
- 357
- 2
- 12
0
votes
1 answer
Binding const reference to another type
How to know if you can bind a const reference T1 to T2 ?
I used to think that you can bind const reference T1 to type T2 only if T2 is convertible to T1.
But since the following compiles:
char x[10];
const char (&y)[10] = x;
that should not be the…

domdrag
- 541
- 1
- 4
- 13
0
votes
0 answers
Eigen::Ref and const_cast
In C++ I can use const_cast to convert a const Eigen::VectorXd& object to an Eigen::VectorXd& one. I wonder if it's possible to do the same with Eigen::Ref objects, i.e., is there a way to convert a const Eigen::Ref& object to…

fdev
- 127
- 12