Questions tagged [reference-binding]

21 questions
37
votes
2 answers

C++ - using const reference to prolong a member of a temporary, ok or UB?

consider something like this: #include struct C { C(double x=0, double y=0): x(x) , y(y) { std::cout << "C ctor " << x << " " <
user2717954
  • 1,822
  • 2
  • 17
  • 28
26
votes
3 answers

Is it ok to return default argument's value by const reference?

Is it ok to return default argument's value by const reference like in the examples below: https://coliru.stacked-crooked.com/a/ff76e060a007723b #include const std::string& foo(const std::string& s = std::string("")) { return s; } int…
19
votes
6 answers

Copy initialization with deleted copy constructor in reference initialization

Consider the follow code: #include class Data{ public: Data() = default; Data(Data const&) = delete; Data(int) { } }; int main(){ int a = 0; const std::string& rs = "abc"; // rs refers to temporary copy-initialized…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
13
votes
2 answers

Does this C++ static analysis rule make sense as is?

I'm implementing some C++ static analysis rules, and one of them prohibits a function from returning a reference or pointer to a reference parameter of the function, i.e. the following are all non-compliant: int *f(int& x) { return &x; } // #1 const…
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
10
votes
1 answer

Is a const reference bound to another reference which is cast from temporary a dangling reference?

Below is the code snippet: #include using namespace std; struct B{ int b; ~B(){cout <<"destruct B" << endl;} }; B func(){ B b; b.b = 1; return b; } int main(){ const B& instance = (const B&)func(); //is…
choxsword
  • 3,187
  • 18
  • 44
5
votes
1 answer

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

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…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
5
votes
2 answers

Why is a double convertible to a const-reference of seemingly any primitive?

Consider the following code: #include float func(char const & val1, unsigned int const & val2) { return val1 + val2; } int main() { double test1 = 0.2; double test2 = 0.3; std::cout << func(test1, test2) << std::endl; …
R_Kapp
  • 2,818
  • 1
  • 18
  • 32
5
votes
1 answer

shared_ptr to const shared_ptr&

I feel confused about shared_ptr, and my main question is: does c++ create a new object (shared_ptr object) when I do the following? void Func1(const shared_ptr& rhs) {} void Func2(const shared_ptr& rhs) {} shared_ptr
4
votes
2 answers

Cast from shared pointer to shared pointer to const

The following code does not compile: #include #include class A { public: A( ) : m_i( new int ) { } std::shared_ptr< const int >& get( ) { return m_i; // <-- invalid initialization of…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
3
votes
1 answer

Does forming a reference to an object constitute access?

Does forming a reference to an object constitute access? Here's what GCC and Clang currently do: void test(int const volatile* ptr) noexcept { *ptr; // movl (%rdi), eax // Reads *ptr [[maybe_unused]] int const volatile& ref = *ptr; // Does…
3
votes
2 answers

Using placement new to update a reference member?

Is the following code legal in C++? template class Foo { public: Foo(T& v) : v_(v) {} private: T& v_; }; int a = 10; Foo f(a); void Bar(int& a) { new (&f)Foo(a); } References are not supposed to be bound twice,…
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
2
votes
1 answer

Best viable conversion function

In the following program, which (if any) conversion function should be selected and why? int r; struct B {}; struct D : B {}; struct S { D d; operator D&(){r=1; return d;} // #1 operator B&(){r=2; return d;} // #2 }; int main() { S s; B& b…
2
votes
1 answer

Non-const lvalue reference to type 'A *' cannot bind to a value of unrelated type 'std::shared_ptr'

First of all, there are bunch of similar posts which appear like they are exactly the same issue, but I find that they are different from my problem. I am trying to use smart pointers for the first time in my C++ life. I have trouble making…
Arun Kumar
  • 634
  • 2
  • 12
  • 26
2
votes
2 answers

Prolonging life of a temporary object using const reference

I need a few clarification regarding const reference. From this link: const Foo &myFoo = FuncBar(); const reference extended the life of the local object. But when I checked this link although they used a const reference Sandbox(const string& n) :…
2
votes
0 answers

Reference initialization and direct vs indirect binding

Consider the following case struct A { operator int(); }; int &&x = A(); The spec says at http://eel.is/c++draft/dcl.init.ref#5 about whether the reference binding is direct or indirect In all cases except the last (i.e., creating and…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
1
2