1

In a function taking an R-value reference, is it well-defined if we take its reference? For example:

void foo(int &&i) {
    cout << &i << endl;
}
int main() {
    int i = 1;
    cout << &i << endl;
    foo(std::move(i));
    return 0;
}

Both cout print the same value, but is it well-defined or undefined?

Or like this:

void foo(int &&i) {
    cout << &i << endl;
}
int main() {
    foo(1);
    return 0;
}

cout prints some address, but is it valid (not pointing any existing object) or just random value?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Antares
  • 213
  • 1
  • 8
  • 2
    Yes, it's okay. R-value reference variable is l-value. – 273K Aug 17 '22 at 06:17
  • 1
    Note, your aren't taking the reference, you're taking the address. In when not used in a type `&` is the "address of operator" not a reference – Alan Birtles Aug 17 '22 at 06:23

0 Answers0