Overloading question

Hi,

I have the following 2 overloaded functions:

1
2
3
4
5
6
7
8
9
void f(int&& x)
{
	cout << "f(int&&)\n";
}

void f(const int&& x)
{
	cout << "f(const int&&)\n";
}


However the second one is never called. I tried:


1
2
3
4
5
6
7
8
9
10
11
int ret_int()
{
	return 77;
}

void usingOverloads()
{
	f(3);
	f(ret_int());

}



in both cases the first definition of f is called! Can't there be a constant temporary variable?


Regards,
Juan
call it this way

const int x = 3;
f(x);

and I think you only want one & on the reference?

actually, if you take the extra &'s off, I think it works as-is. Why did you double them?
Last edited on
In both cases the first definition of f is called! Can't there be a constant temporary variable?

Yes, there can be, but note that rvalue does not mean "temporary", and that rvalue references to const are typically useless.
1
2
int const x = 42; 
f(std::move(x));

The expression std::move(x) is an xvalue of type int const.

@jonnin:
http://thbecker.net/articles/rvalue_references/section_01.html
Last edited on
Could you give me an example of how to implement a move assignment operator whose class has a destructor that has side effects (like releasing a lock)?


So in a sense, we have drifted into the netherworld of non-deterministic destruction here: a variable has been assigned to, but the object formerly held by that variable is still out there somewhere. That's fine as long as the destruction of that object does not have any side effects that are visible to the outside world. But sometimes destructors do have such side effects. An example would be the release of a lock inside a destructor. Therefore, any part of an object's destruction that has side effects should be performed explicitly in the rvalue reference overload of the copy assignment operator:

X& X::operator=(X&& rhs)
{

  // Perform a cleanup that takes care of at least those parts of the
  // destructor that have side effects. Be sure to leave the object
  // in a destructible and assignable state.

  // Move semantics: exchange content between this and rhs
  
  return *this;
}


-- this was taken from http://thbecker.net/articles/rvalue_references/section_04.html
In this example of a class holding a lock to release on destruction, do we want to release the lock in the assignment operator before exchanging content between this and rhs?


At least doing that we are deterministic about when the lock is released....

I got it. In the sample where an instance of X has a lock to be released upon its destruction, then this lock should be released in the rvalue reference assingment operator, something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
X& X::operator=(X&& rhs)
{

  // Perform a cleanup that takes care of at least those parts of the
  // destructor that have side effects. Be sure to leave the object
  // in a destructible and assignable state
  
  this->unlock();

  // Move semantics: exchange content between this and rhs
  
  return *this;
}


Is this correct? Is this what is meant by the text?


Regards,
Juan Dent
Last edited on
Topic archived. No new replies allowed.