The rules for deducing template arguments for the conversion function templates are in the section temp.deduct.conv.
The first rule designates what is the corresponding P and A, the first question is here:
Template argument deduction is done by comparing the return type of the conversion function template (call it P) with the type specified by the conversion-type-id of the conversion-function-id being looked up (call it A) as described in [temp.deduct.type]. If the conversion-function-id is constructed during overload resolution ([over.match.funcs]), the following transformations apply.
#include <iostream>
struct C{
template<typename Type>
operator Type(){
Type a{};
/*a = 1;*/
return a;
}
};
int main(){
int const& rf = C{};
}
For this example, P should be Type but what's the A? It seems there's only one rule related to it:
over#match.funcs.general-7
In each case where conversion functions of a class S are considered for initializing an object or reference of type T, the candidate functions include the result of a search for the conversion-function-id operator T in S.
I don't know whether the A is only int const& here or any of the permissible types defined for this initialization.
If A is determined by the former, then P is Type and A is int const&.
According to bullet 2:
If P is a reference type, the type referred to by P is used in place of P for type deduction and for any further references to or transformations of P in the remainder of this subclause.
Hence, the transformed P is still Type, then such a P/A pair will undergo bullet 3
If A is not a reference type:
- [...]
Because A is of reference type, so skip bullet 3. Continue to bullet 4
If A is a cv-qualified type, the top-level cv-qualifiers of A's type are ignored for type deduction. If A is a reference type, the type referred to by A is used for type deduction.
Undergo bullet 4, now A is transformed to be int const.
Bullet 5 says:
In general, the deduction process attempts to find template argument values that will make the deduced A identical to A. However, certain attributes of A may be ignored:
- If the original A is a reference type, any cv-qualifiers of A (i.e., the type referred to by the reference).
- If the original A is a function pointer or pointer-to-member-function type, its noexcept.
- Any cv-qualifiers in A that can be restored by a qualification conversion.
These attributes are ignored only if type deduction would otherwise fail.
There's a question here, the compiler says the deduced A is int, However, IIUC, deduce Type from const int, Isn't the deduced type would be const int? why the deduced template argument is int rather than const int?
If change the conversion function template to
#include <iostream>
struct C{
template<typename Type>
operator Type&(){
static Type a{};
a = 1;
return a;
}
};
int main(){
int const& rf = C{};
}
Then Type will be deduced to const int. Why the deduced type is different from the above example at this time? (They should have been the same result, which would be const int). Is anything I missed for deducing Type from "const int" in the first example?