jeudi 10 octobre 2019

Why does a function accepting template rvalue Resolve successfully with an lvalue variable (doesn't happen in non-template case)? [duplicate]

Following does not resolve and gives compiler error as expected:

void foo (int&&); // error: cannot bind ‘int’ lvalue to ‘int&&’
int i;
foo(i);

However, following resolves and gives no error:

template<typename T> void foo (T&&); // OK!!!
int i;
foo(i);

Question: How is lvalue binding to rvalue template possible?
Extra: Is there any way to enforce compiler error in case of templates?

[Note:

  • In both the cases, if there is any lvalue overload then that is resolving.
  • Using linux's abi function, printing the type of i gives int]

Aucun commentaire:

Enregistrer un commentaire