Is there a way to make sure that you can't do this
Add(Vector2(100, 100));
but instead only be able to do this
Add(pos);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Vector2 pos(100, 100);
void Add(Vector2& vector)
{
//code
}
int main()
{
//Make sure you can't do this
Add(Vector2(100, 100));
//Only allow this
Add(pos);
}
With the code as it is, you won't be able to do Add( Vector2(100, 100) );
(can't use an rvalue to initialise a reference to non-const; an lvalue is required) http://coliru.stacked-crooked.com/a/842ba05aeba8da88
warning C4239: nonstandard extension used: 'argument': conversion from 'Vector2' to 'Vector2 &'
note: A non-const reference may only be bound to an lvalue
It provides an explicitly deleted overload for rvalues.
If the function is invoked with an rvalue argument, overload resolution would select the deleted function and we get a compile-time error.
Deleted functions
If, instead of a function body, the special syntax = delete ; is used, the function is defined as deleted. Any use of a deleted function is ill-formed (the program will not compile). ...