const-reference

Const reference qualifier on a member function [duplicate]

吃可爱长大的小学妹 提交于 2020-01-14 07:06:39
问题 This question already has answers here : What does the single ampersand after the parameter list of a member function declaration mean? (3 answers) Closed 5 years ago . I have seen in an anwser there: Is returning by rvalue reference more efficient? The member function definition: Beta_ab const& getAB() const& { return ab; } I am familiar with the cv-qualifier ( const ) on member functions, but not const& . What does the last const& mean? 回答1: The & is a ref-qualifier . Ref-qualifiers are new

Const reference qualifier on a member function [duplicate]

假装没事ソ 提交于 2020-01-14 07:04:54
问题 This question already has answers here : What does the single ampersand after the parameter list of a member function declaration mean? (3 answers) Closed 5 years ago . I have seen in an anwser there: Is returning by rvalue reference more efficient? The member function definition: Beta_ab const& getAB() const& { return ab; } I am familiar with the cv-qualifier ( const ) on member functions, but not const& . What does the last const& mean? 回答1: The & is a ref-qualifier . Ref-qualifiers are new

Binding temporary to const reference in c'tor initializer list

扶醉桌前 提交于 2020-01-01 08:13:09
问题 Section 12.2.5 in C++03 says " A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits " So I tried following program #include<iostream> using namespace std; struct foo { foo() { cout<<"foo c'tor"<<endl; } ~foo() { cout<<"foo d'tor"<<endl; } }; struct bar { const foo &ref; bar():ref(foo()) { cout<<"bar c'tor"<<endl; } }; int main() { bar obj; } The output I get is : foo c'tor foo d'tor bar c'tor Now according to standard,

const reference to temporary vs. return value optimization

与世无争的帅哥 提交于 2019-12-31 10:34:16
问题 I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to

const reference to temporary vs. return value optimization

雨燕双飞 提交于 2019-12-31 10:34:12
问题 I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to

C++ Return value, reference, const reference

帅比萌擦擦* 提交于 2019-12-31 08:25:03
问题 Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the

C++ Return value, reference, const reference

一个人想着一个人 提交于 2019-12-31 08:24:49
问题 Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the

Optimize InputIterator dereference without making a copy if possible?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 17:30:37
问题 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 that std::istream_iterators are InputIterator . However they are special among InputIterator s, because their dereference is guarantied to generate a language reference T const& . The code for a general input iterator would look like this, notice that

Will temporary object be deleted if there is no const reference to it?

三世轮回 提交于 2019-12-23 12:13:15
问题 Lets take a look to this two functions: std::string get_string() { std::string ret_value; // Calculate ret_value ... return ret_value; } void process_c_string(const char* s) { std::cout << s << endl; } And here are two possible calls of process_c_string with argument returned by get_string . Without binding const reference to the returned object of get_string . process_c_string(get_string().c_str()); With binding const reference to the returned object of get_string . const std::string& tmp

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

依然范特西╮ 提交于 2019-12-23 04:08:17
问题 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 main(){ node s{3, {4, s}}; assert(s.val == 3); assert(s.next.val == 4); assert(&s.next.next == &s); assert(s.next.next.val == 3); } However, when I try put this as a member of a larger class I get a warning from the compiler and the behavior changes. struct A{