0

This question may be sound stupid. I just want to make sure. And maybe that someone point me where this described in standard.

We cannot have rvalue referenced objects inside lvalue. Right?

struct A{int value;};

struct B{
    B(A &&value) : a(std::forward<A>(value)){}
    A&& a;
};

int main()
{
    // allowed
    B(A()).a;

    // error
    B b(A());
    b.a;

    return 0;
}

http://coliru.stacked-crooked.com/a/ea6bd617d421a8b8

tower120
  • 5,007
  • 6
  • 40
  • 88

1 Answers1

1

B b(A()); declares a function (most vexing parse). You get a compiler error because b has no member a.

To fix the issue write B b{A()} instead.

hansmaad
  • 18,417
  • 9
  • 53
  • 94
  • Yes indeed. So it is allowed to have something like this http://coliru.stacked-crooked.com/a/7612f33c5835fd69 ? I understand that is a dangling reference, and will lead to nowhere. I ask does this allowed by standard? I write rvalue/lvalue cast helper and want to cover all cases. – tower120 Jun 24 '14 at 09:30