const-reference

new-expression and delete-expression on const reference and const pointer

怎甘沉沦 提交于 2019-12-11 22:19:50
问题 C++ Much literature says const references cannot be used to modify their referents and const pointers cannot be used to modify their pointees. Then, why can they be delete d? const int& cirDynamic = *( new int(5) ); // ^ 'const int& cirDynamic = *( &( *( new int(5) ) ) );' gives same output below cout << cirDynamic << endl; // 5 delete &cirDynamic; cout << cirDynamic << endl; // garbage value I know the trailing const in T* const only prevents the pointer from being reseated, but below I use

How to get alternative value from function that gives wanted data via non-const output parameter for assigning reference-to-const variable to it?

ε祈祈猫儿з 提交于 2019-12-11 12:52:30
问题 The commented code works, but it is not a reference, so it has more computational cost. void CClass::Function(const CArray<CItem*>* ItemsInput) const { /* CArray<CItem*> Items; if (ItemsInput != nullptr) Items.Copy(*ItemsInput); else GetContainer().GetInnerItems(Items, NULL, true); */ const CArray<CItem*>& Items= (ItemsInput!= nullptr)? *ItemsInput : [this] () -> const CArray<CItem*> { CArray<CItem*> InnerItems; GetContainer().GetInnerItems(InnerItems, NULL, true); return const_cast <const

Visual Studio is not creating temporary object when typecasting?

吃可爱长大的小学妹 提交于 2019-12-09 11:05:10
问题 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 same type as the reference. #include <iostream> using namespace std; int main() { int number; // float number; number = 2; const int& plainref_i = number; const int& recastref_i = (int)number; // this goes wrong if number is int const float&

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

房东的猫 提交于 2019-12-08 09:49:32
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{ node n; int i; A(int a, int b) : n{a, {b, n}}{} // warning here, also tried n{a, node{b, n}} }; int main()

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

无人久伴 提交于 2019-12-06 11:53:41
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? Quoting from N1377 Bjarne in his excellent text "The Design and Evolution of C++" discusses the motivation for prohibiting the binding of an rvalue to a non-const reference

Is there any C++ compiler which can issue a warning for a dangling reference?

吃可爱长大的小学妹 提交于 2019-12-06 04:39:21
问题 Given the following code, where x is a dangling const reference to a vanished object, and is therefore undefined behavior. auto get_vec() { return std::vector<int>{1,2,3,4,5}; } const auto& x = get_vec().back(); It seems like neither GCC 7.3 , Clang 6.0 and MSVC is able to emit a warning, even with all warnings enabled. Does anyone know if it is any way to emit a warning in these cases? Is there any difference between const auto& and auto&& in these cases? Note, if back() would return by

function call ambiguity with pointer, reference and constant reference parameter

喜你入骨 提交于 2019-12-05 21:01:39
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; A a; a.setS(std::move(s)) //const std::string& .setS("") //const std::string& .setS(s) //std::string&

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

喜欢而已 提交于 2019-12-05 19:38:09
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 ? The C++03 standard ( Section "12.2/5 Temporary objects" ) answers your question aptly: The temporary to which the reference is bound or the

Is there any C++ compiler which can issue a warning for a dangling reference?

…衆ロ難τιáo~ 提交于 2019-12-04 09:07:12
Given the following code, where x is a dangling const reference to a vanished object, and is therefore undefined behavior. auto get_vec() { return std::vector<int>{1,2,3,4,5}; } const auto& x = get_vec().back(); It seems like neither GCC 7.3 , Clang 6.0 and MSVC is able to emit a warning, even with all warnings enabled. Does anyone know if it is any way to emit a warning in these cases? Is there any difference between const auto& and auto&& in these cases? Note, if back() would return by value, it wouldn't be undefined behavior as the lifetime temporary object x is extended to function scoop.

Binding temporary to const reference in c'tor initializer list

元气小坏坏 提交于 2019-12-04 01:05:40
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, temporary generated by foo() in c'tor init-list of bar's c'tor will be destroyed after bar's c'tor so foo d