const-reference

Why reference can not capture temporary while const ref and rval ref can [duplicate]

风格不统一 提交于 2019-12-22 17:40:13
问题 This question already has answers here : How come a non-const reference cannot bind to a temporary object? (11 answers) Closed 5 years ago . Why reference can not capture temporary value while const reference and rvalue reference can capture and prolong object life. In other words while two first lines are legal but third not: const string &a = string("a"); string &&b = string("b"); string &c = string("c"); // why illegal? 回答1: Quoting from N1377 Bjarne in his excellent text "The Design and

function call ambiguity with pointer, reference and constant reference parameter

懵懂的女人 提交于 2019-12-22 10:45:49
问题 What I am trying to do is, allow a pointer, reference or constant reference to be passed with the setter function: class A{ std::string * p; std::string st; public: A():p(0) {} A& setS(const std::string& s){ std::cout<<"called with const std::string&\n"; st = s; p = &st; return *this; } A& setS(std::string& s) { std::cout<<"called with std::string&\n"; p = &s; return *this; } A& setS(std::string* s) { std::cout<<"called with std::string*\n"; p = s; return *this; } }; int main(){ std::string s

What is the lifetime of the class data member which const reference to a rvalue?

只愿长相守 提交于 2019-12-22 08:57:59
问题 Generally this discussion is up to the local function variable only: void foo (const int &i) { // use i till foo() ends } foo(3); But, does this rule applies to the class member also ? struct A { const int &a; A () : a(3) {} // version 1 A (const int &i) : a(i) {} // version 2 }; Now A used as, { return ()? new A : new A(3) : new A(some_local_variable); } Will the contents of a remain same through out the life time of the all 3 new ly allocated A ? 回答1: The C++03 standard ( Section "12.2/5

Reference initialization in C++

筅森魡賤 提交于 2019-12-22 04:26:34
问题 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 temporary of type A Why does const matter here? 回答1: Non-const references must be initialised with l-values. If you could initialise them with temporaries, then what would the following do? int& foo = 5; foo = 6; // ?! const references have the special property that they extend the

Returning const reference of an arraylist

谁说胖子不能爱 提交于 2019-12-21 07:08:39
问题 I really admire java features and I don't want to give up using it for the next problem: I have a class that might be inherited, and inside of it is a private ArrayList arr; So the setter function is ok , but the getter function return arr; returns the reference to that variable which anyone capable of editing that whole array which I don't want and private wouldn't make any sense ! In C++ I would just return const arr; and it would return constant reference to the variable. I so much need

Avoid exponential grow of const references and rvalue references in constructor

青春壹個敷衍的年華 提交于 2019-12-17 15:28:44
问题 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

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

南楼画角 提交于 2019-12-17 12:46:27
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . 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 & sum = a + b; return sum; } But in the following example, the const reference refnop refers to a destroyed temporary object. I

Returning const reference to local variable from a function

时光总嘲笑我的痴心妄想 提交于 2019-12-17 07:05:33
问题 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.

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

佐手、 提交于 2019-12-13 00:39:18
问题 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 the reference. But what about this? #include <iostream> int main() { int a = 0; const int& r = a; const int old_r = r; ++a; const int new_r = r; std::cout << "old_r == " << old_r << " but new_r == " << new_r << std::endl; return 0; } On my machine,

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

断了今生、忘了曾经 提交于 2019-12-12 01:57:06
问题 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