MA Shengjing
MA Shengjing

Reputation: 113

templates function parameter deduction

Here is one exercise from C++ Primer 5th edition:

template<typename T>
void f1(T, T){}
int i = 0, *p1 = &i;
const int *cp1 = &i;

f1(p1, cp1);

But compiler generates an error:

no matching function for call to 'f1(int*&, const int*&)'

I have no idea why the error include a point reference? I think the parameter deduction is 'f1(int *, const int *)'.

Upvotes: 0

Views: 56

Answers (1)

Brian Bi
Brian Bi

Reputation: 119587

That's the way GCC indicates that the argument is an lvalue. If it says the argument type is T&, it means the argument has type T and is an lvalue. If it says the argument type is T (non-reference), it means the argument has type T and is an rvalue.

Upvotes: 3

Related Questions