Disable rvalue binding through constructor to element constant reference

I am working on a class of matrix representations from which the constructor takes the matrix as a parameter and binds it to the const element of the link. I would really like to avoid the rvalues ​​binding, since they do not bind using the constructor parameter, and we end up with a link. I came up with the following (simplified code):

 struct Foo{}; class X { const Foo& _foo; public: X(const Foo&&) = delete; // prevents rvalue binding X(const Foo& foo): _foo(foo){} // lvalue is OK }; Foo get_Foo() { return {}; } const Foo get_const_Foo() { return {}; } Foo& get_lvalue_Foo() { static Foo foo; return foo; } int main() { // X x1{get_Foo()}; // does not compile, use of deleted function // X x2{get_const_Foo()}; // does not compile, use of deleted function X x3{get_lvalue_Foo()}; // this should be OK } 

I basically delete the constructor that takes const Foo&& as a parameter. Note that I need const , because otherwise, someone might return const Foo from the function, in which case it will be bound to the const Foo& constructor.

Question:

Is this the correct paradigm for disabling rvalue bindings? Did I miss something?

+2
c ++ c ++ 11 rvalue-reference
Oct 20 '15 at 19:10
source share

No one has answered this question yet.

See similar questions:

43
Reference member variables as members of a class
8
Passing with reference options in C ++
2
Disable temporary linking of Eigen expression with constant links

or similar:

3076
What are the differences between a pointer variable and a reference variable in C ++?
37
Avoid exponential growth of const references and rvalue references in the constructor
31
Should the move constructor refer to a constant or not a constant?
27
C ++ 0x const RValue reference as function parameter
10
C ++ 0x: rvalue reference versus non lvalue constant
four
Would you ever mark a C ++ RValue reference parameter as const
2
Disable temporary linking of Eigen expression with constant links
2
Generic copy constructor with reference element numbers
one
Confusion between rvalue references and const lvalue references as parameter
0
A rvalue reference can only be associated with non-constant r values?



All Articles