Why can C++ deduce template arguments on the right side of an assignment operator from the left side?

Is this standard behavior?

Yes it is. Template argument deduction also happens when you take the address of a function template (such as you do when assigning to or initializing a function pointer). It’s explicitly allowed in [temp.deduct.funcaddr]/1:

Template arguments can be deduced from the type specified when taking
the address of an overloaded function. The function template’s
function type and the specified type are used as the types of P and A,
and the deduction is done as described in [temp.deduct.type].

The function pointer type provides the argument (A in the above paragraph).

So func is actually deduced from the input argument type of operator=, rather than from type of p directly. Is that correct?

Not really. For one, it’s not assignment, it’s initialization that you are doing. And even if it was using an overloaded operator= function, you’d need deduction to initialize the argument for the assignment operator, which brings you back to square one.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)