0

This is really two questions about ref rolled into one:

  1. Can I use ref on an rvalue? Obviously as the programmer I would have the responsibility to ensure it outlived any calling code.
  2. Is there a pointer version of ref?

The goal of these two sketchy questions are realized in this sketchy answer: https://stackoverflow.com/a/29031944/2642059 Where I'd rather just create a pointer to the vector::data rvalue than create a temp variable.

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • As per the linked cpp std lib specification no you cannot use ref on an rvalue. I don't understand what you want to do with 2) – Félix Cantournet Mar 13 '15 at 12:42
  • 1. Yes, it is called an rvalue reference in C++11 ;) 2. No, references are essentially pointers that can never be null, and some added syntactic sugar. They are necessary for the C++ language design, imagine e.g. a copy constructor or operator overloading without them. – Erik Alapää Mar 13 '15 at 12:43

1 Answers1

4
  1. No, the interface prevents that, as the reference you link to shows. There's an overload taking an lvalue reference; the one taking an rvalue reference is deleted. It's hard to imagine how you might be able to take responsibility for the object's lifetime without being able to access it via an lvalue.

  2. That would be a pointer. The purpose of reference_wrapper is to allow references to be passed around like values (which regular references can't be), while preserving the syntax of a reference (which you'd lose by using a pointer instead). If you want to pass a pointer by value, and have it act like a pointer, then just use a pointer.

I'd rather just create a pointer to the vector::data rvalue than create a temp variable.

That would be a pointer to a temporary. The temporary will be destroyed, invalidating the pointer, before you can do anything with it. As you say, you need to take responsibility for its lifetime, which you do by creating a variable. Once you have that, you have an lvalue.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Let me clarify now that my linked answer of wrath has been deleted: Say I have a function with this signature: `void foo(vector*)` If I create a vector inline I know that it will out live `foo`'s stack lifespan. But I want to pass a pointer to it. Is there a way to do this? – Jonathan Mee Mar 17 '15 at 12:17
  • 1
    @JonathanMee: No. You could change the function to take a value or a constant or _rvalue_ reference, or you could pass a pointer to a non-temporary. But the language won't let you take the address of a temporary even in cases where you can avoid dangling pointers. – Mike Seymour Mar 17 '15 at 12:28
  • Thanks, that's the answer I was looking for. Any chance you could edit your answer to include that information before I accept it? – Jonathan Mee Mar 17 '15 at 12:32
  • @JonathanMee: A comment's a better place for something that isn't related to the question (which already has one question too many). – Mike Seymour Mar 17 '15 at 12:34