For example:
template <typename T> void g(T &&val);
int i = 0; const int ci = i;
g(i = ci);
What is the template argument of g?
As per §5.18/1:
The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand. [...]
So, in
g(i = ci)
the left operand, i, is returned and therefore T is deduced to int&.
You can check this via this snippet:
#include <type_traits>
template <typename T>
void g(T &&val) {
static_assert(std::is_same<T, int&>::value, "Nope");
}
int main() {
int i = 0; const int ci = i;
g(i = ci);
}
Live demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With