const-reference

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

◇◆丶佛笑我妖孽 提交于 2019-11-29 10:16:49
问题 Consider this code, struct A {}; struct B { B(const A&) {} }; void f(B) { cout << "f()"<<endl; } void g(A &a) { cout << "g()" <<endl; f(a); //a is implicitly converted into B. } int main() { A a; g(a); } This compiles fine, runs fine. But if I change f(B) to f(B&) , it doesn't compile. If I write f(const B&) , it again compiles fine, runs fine. Why is the reason and rationale? Summary: void f(B); //okay void f(B&); //error void f(const B&); //okay I would like to hear reasons, rationale and

const-ref when sending signals in Qt

隐身守侯 提交于 2019-11-28 22:24:55
问题 This is a thing that I never quite got with const-ref and I really hope that someone could explain it to me. When calling a function inside of another function, I get that const-ref is the best way when passing stack objects that I don't plan to tamper with. For example: void someInnerFunction(const QString& text) { qDebug() << text; } void someFunction() { QString test = "lala"; .... someInnerFunction(test); } So far so good, I guess. But what about signals? Isn't there any risk that comes

Passing rvalue reference to const lvalue reference paremeter

限于喜欢 提交于 2019-11-28 21:42:43
问题 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) that does something with an object of class A . I want to prevent the copy constructor of A from being called when an object of A is passed to the function foo , since in that case it will perform a deep copy of the underlying heap data. I tested

Why is `const T&` not sure to be const?

本小妞迷上赌 提交于 2019-11-28 19:30:15
问题 template<typename T> void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template<typename T> void g(T n) { f<T>(n, n); } int main() { int n{}; g<int&>(n); } Please note: b is of const T& and ++b is ok! Why is const T& not sure to be const? 回答1: Welcome to const and reference collapsing. When you have const T& , the reference gets applied to T , and so does the const . You call g like g<int&>(n); so you have specified that T is a int& . When we apply a reference to an lvalue reference,

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

半世苍凉 提交于 2019-11-28 01:00:26
问题 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" << std::endl; delete i; } void get() { std::cout << *i << std::endl; } private: int* i; }; const A& foo() { return A(32); } const A& foo_2() { return 6; } int main() { A a = foo(); a.get(); } I know, returning references to local values is bad. But, on the other hand, const reference should extend a

Avoid exponential grow of const references and rvalue references in constructor

假如想象 提交于 2019-11-27 18:07:43
I am coding some templated classes for a machine learning library, and I'm facing this issue a lot of times. I'm using mostly the policy pattern, where classes receive as template argument policies for different functionalities, for example: template <class Loss, class Optimizer> class LinearClassifier { ... } The problem is with the constructors. As the amount of policies (template parameters) grows, the combinations of const references and rvalue references grow exponentially. In the previous example: LinearClassifier(const Loss& loss, const Optimizer& optimizer) : _loss(loss), _optimizer

Returning const reference to local variable from a function

佐手、 提交于 2019-11-27 02:55:40
I have some questions on returning a reference to a local variable from a function: class A { public: A(int xx) : x(xx) { printf("A::A()\n"); } }; const A& getA1() { A a(5); return a; } A& getA2() { A a(5); return a; } A getA3() { A a(5); return a; } int main() { const A& newA1 = getA1(); //1 A& newA2 = getA2(); //2 A& newA3 = getA3(); //3 } My questions are => Is the implementation of getA1() correct? I feel it is incorrect as it is returning the address of a local variable or temporary. Which of the statements in main (1,2,3) will lead to undefined behavior? In const A& newA1 = getA1(); does

Does a const reference class member prolong the life of a temporary?

风格不统一 提交于 2019-11-25 21:47:45
问题 Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string(\"four\")); cout << \"The answer is: \" << sandbox.member << endl; return 0; } Give output of: The answer is: Instead of: The answer is: four 回答1: Only local const references prolong the lifespan. The standard specifies such behavior in §8.5.3/5, [dcl.init.ref], the section on initializers of