jeudi 3 septembre 2015

Calling function based on the value categories of its arguments

Yesterday I asked a question about when to use std::forward and when to use std::move

Today I was trying to apply what I think I learned. I wrote the following:

template <typename T>
void exp(T a, T b)
{
  cout << "rvalues" << endl;
}

template <typename T>
void exp(T& a, T& b)
{
  cout << "lvalues" << endl;
}

template <typename T>
void foo(T&& a, T&& b)
{
  exp(forward<T>(a), forward<T>(b));
}

When in the main I call foo(4, 5) it prints out "rvalue", as I would expect, but when I do something like

int a = 0, b = 0;
foo(a, b);

an error occurs that says: 'exp' : ambiguous call to overloaded function

What am I missing here?

Aucun commentaire:

Enregistrer un commentaire