Questions tagged [const-reference]

131 questions
12
votes
1 answer

Passing rvalue reference to const lvalue reference paremeter

I am trying to understand C++11 rvalue references and how to use them for optimal performance in my code. Let's say we have a class A that has a member pointer to a large amount of dynamically allocated data. Furthermore, a method foo(const A& a)…
Sven
  • 143
  • 1
  • 1
  • 7
11
votes
2 answers

Implicit conversion : const reference vs non-const reference vs non-reference

Consider this code, struct A {}; struct B { B(const A&) {} }; void f(B) { cout << "f()"<
Nawaz
  • 353,942
  • 115
  • 666
  • 851
11
votes
3 answers

Why can a const reference to a string parameter take string literals?

Why can a const reference to a string parameter take string literals? String literals, like "hello", are not variables, so why is this code valid? class CVector { public: int x, y; CVector() {}; ~CVector() { delete ptr;…
user7982333
11
votes
4 answers

Binding const& of temporary: No compiler warning?

I have a TestClass with a const& member variable. I know from various places and own experiences that it is a bad idea to initialize this const& with the reference to a temporary value. So I was quite suprised that the following code will compile…
user3520187
  • 168
  • 7
10
votes
5 answers

Reference initialization in C++

Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a…
skydoor
  • 25,218
  • 52
  • 147
  • 201
9
votes
2 answers

Variadic template specialization with const reference

How to specialize variadic template function that has const reference for an argument? Example: template T foo(Args... args) = delete; template<> int foo(int a, const char* str, const Test& t) { .... } // Fails to…
9
votes
7 answers

How to return a const QString reference in case of failure?

consider the following code: const QString& MyClass::getID(int index) const { if (i < myArraySize && myArray[i]) { return myArray[i]->id; // id is a QString } else { return my_global_empty_qstring; // is a global empty…
moala
  • 5,094
  • 9
  • 45
  • 66
9
votes
2 answers

What exactly happens when returning const reference to a local object?

struct A { A(int) : i(new int(783)) { std::cout << "a ctor" << std::endl; } A(const A& other) : i(new int(*(other.i))) { std::cout << "a copy ctor" << std::endl; } ~A() { std::cout << "a dtor" <<…
Alexander
  • 779
  • 8
  • 17
8
votes
2 answers

Prevent temporary from extending its lifetime?

This may be impossible, but I was wondering if it was possible to keep a temporary from ever lasting past its original expression. I have a chain of objects which point to parent objects, and a member function which will create a child object, a…
zounds
  • 773
  • 8
  • 17
8
votes
3 answers

Why does this call by reference create a new instance?

Im calling a method foo by const ref: // method, which is being called void foo(const Entity & ent); // call Entity* e = new Entity; foo(e); // wrong: missing * but compiles This piece of code does not only compile, it creates a new instance of…
atamanroman
  • 11,607
  • 7
  • 57
  • 81
8
votes
4 answers

Warning C4172: Returning a reference to const std::string bound to a local variable. How safe is it?

I was just building one of our projects at work and I see a new function was added: const std::string& ClassName::MethodName() const { return ""; } The compiler gives a warning: Warning C4172: returning address of local variable or temporary I…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
7
votes
1 answer

Best practice for getters with ref-qualifier

The following code causes undefined behaviour: class T { public: const std::string& get() const { return s_; } private: std::string s_ { "test" }; } void breaking() { const auto& str = T{}.get(); // do sth with "str" <--…
Tim
  • 143
  • 1
  • 5
7
votes
1 answer

Visual Studio is not creating temporary object when typecasting?

I'm using Visual Studio Express 2013 and is fooling around a bit trying to learn about different things in C++. I stumbled upon an interesting bug in the compiler where it doesn't seem to create a temporary object when explicitly type casting to the…
6
votes
1 answer

Is the lifetime of a C++ temporary object created in ?: expression extended by binding it to a local const reference?

It is not clear to me whether the lifetime of a temporary object would be extended by binding it to a const reference in a ?: expression: class Foo {...}; Foo *someLValue = ...; const Foo& = someLValue ? *someLValue : Foo(); Is the lifetime of…
Palo
  • 1,051
  • 8
  • 12
6
votes
2 answers

const reference to a temporary object becomes broken after function scope (life time)

While asking this question, I learned const reference to a temporary object is valid in C++: int main () { int a = 21; int b = 21; //error: invalid initialization of non-const reference //int & sum = a + b;e [...] //OK int const &…
oHo
  • 51,447
  • 27
  • 165
  • 200
1
2
3
8 9