unify (continue)
9916 case TEMPLATE_PARM_INDEX:
9917 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9918
9919 if (TEMPLATE_PARM_LEVEL (parm)
9920 != template_decl_level (tparm))
9921 /* The PARM is not one we're trying to unify. Just check
9922 to see if it matches ARG. */
9923 return !(TREE_CODE (arg) == TREE_CODE (parm)
9924 && cp_tree_equal (parm, arg));
9925
9926 idx = TEMPLATE_PARM_IDX (parm);
9927 targ = TREE_VEC_ELT (targs, idx);
9928
9929 if (targ)
9930 return !cp_tree_equal (targ, arg);
9931
9932 /* [temp.deduct.type] If, in the declaration of a function template
9933 with a non-type template-parameter, the non-type
9934 template-parameter is used in an expression in the function
9935 parameter-list and, if the corresponding template-argument is
9936 deduced, the template-argument type shall match the type of the
9937 template-parameter exactly, except that a template-argument
9938 deduced from an array bound may be of any integral type.
9939 The non-type parameter might use already deduced type parameters. */
9940 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9941 if (!TREE_TYPE (arg))
9942 /* Template-parameter dependent expression. Just accept it for now.
9943 It will later be processed in convert_template_argument. */
9944 ;
9945 else if (same_type_p (TREE_TYPE (arg), tparm))
9946 /* OK */ ;
9947 else if ((strict & UNIFY_ALLOW_INTEGER)
9948 && (TREE_CODE (tparm) == INTEGER_TYPE
9949 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9950 /* Convert the ARG to the type of PARM; the deduced non-type
9951 template argument must exactly match the types of the
9952 corresponding parameter. */
9953 arg = fold (build_nop (TREE_TYPE (parm), arg));
9954 else if (uses_template_parms (tparm))
9955 /* We haven't deduced the type of this parameter yet. Try again
9956 later. */
9957 return 0;
9958 else
9959 return 1;
9960
9961 TREE_VEC_ELT (targs, idx) = arg;
9962 return 0;
Above at line 9812, if tparm refers to TEMPLATE_PARM_INDEX, which we can see clearly in examples explaining the behavior of parser (refer to section The second example ). Field TEMPLATE_PARM_IDX (short for IDX below) gives the index (from 0) of the parameter, while field TEMPLATE_PARM_LEVEL (short for LEVEL below) gives the level (from 1) of the parameter. For example:
template <class T> // Index 0, Level 1
struct S {
template <class U, // Index 0, Level 2
class V> // Index 1, Level 2
void f();
};
Three TEMPLATE_PARM_INDEXs will be generated in the example. In these nodes, the TEMPLATE_PARM_DESCENDANTS field contains a chain of TEMPLATE_PARM_INDEXs descended from them. The first descendant will have the same IDX, but its LEVEL will be one less. The TREE_CHAIN field is used to chain together the descendants. In descendants, field TEMPLATE_PARM_DECL is the declaration of this parameter, either a TYPE_DECL or CONST_DECL. Field TEMPLATE_PARM_ORIG_LEVEL (short for ORIG_LEVEL below) is the LEVEL of the most distant parent, i.e., the LEVEL that the parameter originally had when it was declared. For example, if we instantiate S
struct S
template <class U, // Index 0, Level 1, Orig Level 2
class V> // Index 1, Level 1, Orig Level 2
void f();
};
The LEVEL is the level of the parameter when we are worrying about the types of things; the ORIG_LEVEL is the level when we are worrying about instantiating things.
Note that targs is the vector that will hold the deduced argument, if the corresponding slot is not NULL, it means the argument has been deduced (or explicitly given). Of course, the deduced argument must be the same as the argument already deduced here (which means two deductions of one template parameter). Also see that targs is unchanged within unify and its recursions. So it subsistitutes these known arguments into the template specified by parm at line 9940, and makes tparm refer to thus deduced argument. For success deduction, no doubt, tparm and arg must have same type (except case of using UNIFY_ALLOW_INTEGER).
Notice line 9941, if TREE_TYPE is NULL, arg should be an IDENTIFIER_NODE, like ‘T’. And if tparm deduced at line 9940 still depends on template parameter, these undeduced parameters may be resolved when returns back try_one_overload or found as error. It can safely accept it for the moment.
unify (continue)
9964 case PTRMEM_CST:
9965 {
9966 /* A pointer-to-member constant can be unified only with
9967 another constant. */
9968 if (TREE_CODE (arg) != PTRMEM_CST)
9969 return 1;
9970
9971 /* Just unify the class member. It would be useless (and possibly
9972 wrong, depending on the strict flags) to unify also
9973 PTRMEM_CST_CLASS, because we want to be sure that both parm and
9974 arg refer to the same variable, even if through different
9975 classes. For instance:
9976
9977 struct A { int x; };
9978 struct B : A { };
9979
9980 Unification of &A::x and &B::x must succeed. */
9981 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
9982 PTRMEM_CST_MEMBER (arg), strict);
9983 }
9984
9985 case POINTER_TYPE:
9986 {
9987 if (TREE_CODE (arg) != POINTER_TYPE)
9988 return 1;
9989
9990 /* [temp.deduct.call]
9991
9992 A can be another pointer or pointer to member type that can
9993 be converted to the deduced A via a qualification
9994 conversion (_conv.qual_).
9995
9996 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9997 This will allow for additional cv-qualification of the
9998 pointed-to types if appropriate. */
9999
10000 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10001 /* The derived-to-base conversion only persists through one
10002 level of pointers. */
10003 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10004
10005 return unify (tparms, targs, TREE_TYPE (parm),
10006 TREE_TYPE (arg), strict);
10007 }
10008
10009 case REFERENCE_TYPE:
10010 if (TREE_CODE (arg) != REFERENCE_TYPE)
10011 return 1;
10012 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10013 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10014
10015 case ARRAY_TYPE:
10016 if (TREE_CODE (arg) != ARRAY_TYPE)
10017 return 1;
10018 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10019 != (TYPE_DOMAIN (arg) == NULL_TREE))
10020 return 1;
10021 if (TYPE_DOMAIN (parm) != NULL_TREE
10022 && unify (tparms, targs, TYPE_DOMAIN (parm),
10023 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
10024 return 1;
10025 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10026 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10027
10028 case REAL_TYPE:
10029 case COMPLEX_TYPE:
10030 case VECTOR_TYPE:
10031 case INTEGER_TYPE:
10032 case BOOLEAN_TYPE:
10033 case ENUMERAL_TYPE:
10034 case VOID_TYPE:
10035 if (TREE_CODE (arg) != TREE_CODE (parm))
10036 return 1;
10037
10038 if (TREE_CODE (parm) == INTEGER_TYPE
10039 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
10040 {
10041 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
10042 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
10043 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
10044 return 1;
10045 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
10046 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
10047 TYPE_MAX_VALUE (arg),
10048 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
10049 return 1;
10050 }
10051 /* We have already checked cv-qualification at the top of the
10052 function. */
10053 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10054 return 1;
10055
10056 /* As far as unification is concerned, this wins. Later checks
10057 will invalidate it if necessary. */
10058 return 0;
10059
10060 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10061 /* Type INTEGER_CST can come from ordinary constant template args. */
10062 case INTEGER_CST:
10063 while (TREE_CODE (arg) == NOP_EXPR)
10064 arg = TREE_OPERAND (arg, 0);
10065
10066 if (TREE_CODE (arg) != INTEGER_CST)
10067 return 1;
10068 return !tree_int_cst_equal (parm, arg);
10069
10070 case TREE_VEC:
10071 {
10072 int i;
10073 if (TREE_CODE (arg) != TREE_VEC)
10074 return 1;
10075 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10076 return 1;
10077 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10078 if (unify (tparms, targs,
10079 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10080 UNIFY_ALLOW_NONE))
10081 return 1;
10082 return 0;
10083 }
Have a look at comment at line 9971, it explains why deducing PTRMEM_CST_MEMBER instead of the whole structure. And note for the types of scalar quantity (from REAL_TYPE to ENUMERATE_TYPE, and VOID_TYPE is a deviant, it should not be used as template parameter, but can be passed in as default argument), they correspond to non-type template parameter (excluding VOID_TYPE).
unify (continue)
10085 case RECORD_TYPE:
10086 case UNION_TYPE:
10087 if (TREE_CODE (arg) != TREE_CODE (parm))
10088 return 1;
10089
10090 if (TYPE_PTRMEMFUNC_P (parm))
10091 {
10092 if (!TYPE_PTRMEMFUNC_P (arg))
10093 return 1;
10094
10095 return unify (tparms, targs,
10096 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10097 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10098 strict);
10099 }
10100
10101 if (CLASSTYPE_TEMPLATE_INFO (parm))
10102 {
10103 tree t = NULL_TREE;
10104
10105 if (strict_in & UNIFY_ALLOW_DERIVED)
10106 {
10107 /* First, we try to unify the PARM and ARG directly. */
10108 t = try_class_unification (tparms, targs,
10109 parm, arg);
10110
10111 if (!t)
10112 {
10113 /* Fallback to the special case allowed in
10114 [temp.deduct.call]:
10115
10116 If P is a class, and P has the form
10117 template-id, then A can be a derived class of
10118 the deduced A. Likewise, if P is a pointer to
10119 a class of the form template-id, A can be a
10120 pointer to a derived class pointed to by the
10121 deduced A. */
10122 t = get_template_base (tparms, targs,
10123 parm, arg);
10124
10125 if (! t || t == error_mark_node)
10126 return 1;
10127 }
10128 }
10129 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10130 && (CLASSTYPE_TI_TEMPLATE (parm)
10131 == CLASSTYPE_TI_TEMPLATE (arg)))
10132 /* Perhaps PARM is something like S and ARG is S
10133 Then, we should unify `int' and `U'. */
10134 t = arg;
10135 else
10136 /* There's no chance of unification succeeding. */
10137 return 1;
10138
10139 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10140 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10141 }
10142 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10143 return 1;
10144 return 0;
Above line 10101, CLASSTYPE_TEMPLATE_INFO if non-null, indicates parm is a class template. See that strict_in copies argument strict at line 9741 in the function, in which UNIFY_ALLOW_DERIVED is set in type_unification_real when deducing template argument by function call (refers to paragraphes before maybe_adjust_types_for_deduction ).
9486 static tree
9487 try_class_unification (tree tparms, tree targs, tree parm, tree arg) in pt.c
9488 {
9489 tree copy_of_targs;
9490
9491 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9492 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9493 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9494 return NULL_TREE;
9495
9496 /* We need to make a new template argument vector for the call to
9497 unify. If we used TARGS, we'd clutter it up with the result of
9498 the attempted unification, even if this class didn't work out.
9499 We also don't want to commit ourselves to all the unifications
9500 we've already done, since unification is supposed to be done on
9501 an argument-by-argument basis. In other words, consider the
9502 following pathological case:
9503
9504 template
9505 struct S {};
9506
9507 template
9508 struct S : public S, S
9509
9510 template
9511 void f(S, S);
9512
9513 void g() {
9514 S<0, 0, 0> s0;
9515 S<0, 1, 2> s2;
9516
9517 f(s0, s2);
9518 }
9519
9520 Now, by the time we consider the unification involving `s2', we
9521 already know that we must have `f<0, 0, 0>'. But, even though
9522 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9523 because there are two ways to unify base classes of S<0, 1, 2>
9524 with S. If we kept the already deduced knowledge, we
9525 would reject the possibility I=1. */
9526 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9527
9528 /* If unification failed, we're done. */
9529 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9530 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9531 return NULL_TREE;
9532
9533 return arg;
9534 }
It is a long comment to explain why using a copy of template argument vector, and gives an example which is error. Further, if swaps s0 and s2 for f at line 9517, it is a good code. S<0, 1, 2> is not good for parameter S, because S<0, 1, 2> derives from S<0, 0, 0> and S<1, 1, 1>, both which can match the form of S. Recall that [3] defines that argument deduction is done argument by argument, and after deducing, the arguments deduced should match the version already deduced before or explicitly given. But here the invocation of unify is just a try, by using the template argument vector copy, it won’t change the already deduced arguments.
For the example given by the comment above, s2 is template other than S, so try_class_unification returns at line 9494; and entering below function to find base that can be unified (as now UNIFY_ALLOW_DERIVED is used).
9614 static tree
9615 get_template_base (tree tparms, tree targs, tree parm, tree arg) in pt.c
9616 {
9617 tree rval;
9618 tree arg_binfo;
9619
9620 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
9621
9622 arg_binfo = TYPE_BINFO (complete_type (arg));
9623 rval = get_template_base_recursive (tparms, targs,
9624 parm, arg_binfo,
9625 NULL_TREE,
9626 GTB_IGNORE_TYPE);
9627
9628 /* Since get_template_base_recursive marks the bases classes, we
9629 must unmark them here. */
9630 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
9631
9632 return rval;
9633 }
The function traverses the class hierarchy tree and executes try_class_unification upon base in pre-order. See that for s2 , both its base S<0, 0, 0> and S<1, 1, 1> enter try_class_unification at line 9554 in turn. But the unequal S<0, 0, 0> and S<1, 1, 1> referred by rval and r respectively in last recursion causes the function returns error_mark_node at line 9566.
9540 static tree
9541 get_template_base_recursive (tree tparms, in pt.c
9542 tree targs,
9543 tree parm,
9544 tree arg_binfo,
9545 tree rval,
9546 int flags)
9547 {
9548 tree binfos;
9549 int i, n_baselinks;
9550 tree arg = BINFO_TYPE (arg_binfo);
9551
9552 if (!(flags & GTB_IGNORE_TYPE))
9553 {
9554 tree r = try_class_unification (tparms, targs,
9555 parm, arg);
9556
9557 /* If there is more than one satisfactory baseclass, then:
9558
9559 [temp.deduct.call]
9560
9561 If they yield more than one possible deduced A, the type
9562 deduction fails.
9563
9564 applies. */
9565 if (r && rval && !same_type_p (r, rval))
9566 return error_mark_node;
9567 else if (r)
9568 rval = r;
9569 }
9570
9571 binfos = BINFO_BASETYPES (arg_binfo);
9572 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
9573
9574 /* Process base types. */
9575 for (i = 0; i < n_baselinks; i++)
9576 {
9577 tree base_binfo = TREE_VEC_ELT (binfos, i);
9578 int this_virtual;
9579
9580 /* Skip this base, if we've already seen it. */
9581 if (BINFO_MARKED (base_binfo))
9582 continue ;
9583
9584 this_virtual =
9585 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
9586
9587 /* When searching for a non-virtual, we cannot mark virtually
9588 found binfos. */
9589 if (! this_virtual)
9590 BINFO_MARKED (base_binfo) = 1;
9591
9592 rval = get_template_base_recursive (tparms, targs,
9593 parm,
9594 base_binfo,
9595 rval,
9596 GTB_VIA_VIRTUAL * this_virtual);
9597
9598 /* If we discovered more than one matching base class, we can
9599 stop now. */
9600 if (rval == error_mark_node)
9601 return error_mark_node;
9602 }
9603
9604 return rval;
9605 }
Back unify , if either try_class_unification at line 10108, or get_template_base at line 10122 successes, it means the deduction will be done successfully; thus does the real deduction at line 10139.
Then at line 10142, for normal class, arg and parm should be the same type except the top cv-qualifier.
Below as terms 3 in [temp.deduct.type] tells:
“A function type includes the types of each of the function parameters and the return type.
A pointer to member type includes the type of the class object pointed to and the type of the member pointed to.”
The code handles the cases accordingly. And further down, node MINUS_EXPR may come with ARRAY_TYPE, the comment associated gives a clear explaination.
unify (continue)
10146 case METHOD_TYPE:
10147 case FUNCTION_TYPE:
10148 if (TREE_CODE (arg) != TREE_CODE (parm))
10149 return 1;
10150
10151 if (unify (tparms, targs, TREE_TYPE (parm),
10152 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10153 return 1;
10154 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10155 TYPE_ARG_TYPES (arg), 1,
10156 DEDUCE_EXACT, 0, -1);
10157
10158 case OFFSET_TYPE:
10159 if (TREE_CODE (arg) != OFFSET_TYPE)
10160 return 1;
10161 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10162 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10163 return 1;
10164 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10165 strict);
10166
10167 case CONST_DECL:
10168 if (DECL_TEMPLATE_PARM_P (parm))
10169 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10170 if (arg != decl_constant_value (parm))
10171 return 1;
10172 return 0;
10173
10174 case FIELD_DECL:
10175 case TEMPLATE_DECL:
10176 /* Matched cases are handled by the ARG == PARM test above. */
10177 return 1;
10178
10179 case MINUS_EXPR:
10180 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
10181 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
10182 {
10183 /* We handle this case specially, since it comes up with
10184 arrays. In particular, something like:
10185
10186 template
10187
10188 Here, we are trying to unify the range type, which
10189 looks like [0 ... (N - 1)]. */
10190 tree t, t1, t2;
10191 t1 = TREE_OPERAND (parm, 0);
10192 t2 = TREE_OPERAND (parm, 1);
10193
10194 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
10195
10196 return unify (tparms, targs, t1, t, strict);
10197 }
10198 /* Else fall through. */
10199
10200 default :
10201 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
10202 {
10203
10204 /* We're looking at an expression. This can happen with
10205 something like:
10206
10207 template
10208 void foo(S, S);
10209
10210 This is a "nondeduced context":
10211
10212 [deduct.type]
10213
10214 The nondeduced contexts are:
10215
10216 --A type that is a template-id in which one or more of
10217 the template-arguments is an expression that references
10218 a template-parameter.
10219
10220 In these cases, we assume deduction succeeded, but don't
10221 actually infer any unifications. */
10222
10223 if (!uses_template_parms (parm)
10224 && !template_args_equal (parm, arg))
10225 return 1;
10226 else
10227 return 0;
10228 }
10229 sorry ("use of `%s' in template type unification",
10230 tree_code_name [(int) TREE_CODE (parm)]);
10231 return 1;
10232 }
10233 }
At last, it enters the default clause if it is an expression forming nondeduced context. Terms 4 in [temp.deduct.type] gives the definition by the standard. Here just one of them; and the other is handled at the head of the switch block at line 9789.
try_one_overload (continue)
9421 /* First make sure we didn't deduce anything that conflicts with
9422 explicitly specified args. */
9423 for (i = nargs; i--; )
9424 {
9425 tree elt = TREE_VEC_ELT (tempargs, i);
9426 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9427
9428 if (elt == NULL_TREE)
9429 continue ;
9430 else if (uses_template_parms (elt))
9431 {
9432 /* Since we're unifying against ourselves, we will fill in template
9433 args used in the function parm list with our own template parms.
9434 Discard them. */
9435 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9436 continue ;
9437 }
9438 else if (oldelt && ! template_args_equal (oldelt, elt))
9439 return 0;
9440 }
9441
9442 for (i = nargs; i--; )
9443 {
9444 tree elt = TREE_VEC_ELT (tempargs, i);
9445
9446 if (elt)
9447 TREE_VEC_ELT (targs, i) = elt;
9448 }
9449
9450 return 1;
9451 }
If deduction of the pair successes, at this point, tempargs contains the argument deduced this time, and orig_targs contains arguments that deduced before. If both corresponding entries are not null and independent upon template parameter, they must be the same. If everything is OK, it finally merges the result in tempargs with targs .
Remember try_one_overload is invoked by resolve_overloaded_unification to deduce certain argument suspected being pointer to overload. But for other arguments, their are handled by the block at 9229 in type_unification_real . Note at line 9240, len comes from argument xlen which specifies the number of function parmeters to consider before returning success if it is larger than 0. If all arguments are handled successfully, we enter below code.
type_unification_real (continue)
9243 /* Fail if we've reached the end of the parm list, and more args
9244 are present, and the parm list isn't variadic. */
9245 if (args && args != void_list_node && parms == void_list_node)
9246 return 1;
9247 /* Fail if parms are left and they don't have default values. */
9248 if (parms
9249 && parms != void_list_node
9250 && TREE_PURPOSE (parms) == NULL_TREE)
9251 return 1;
9252
9253 done:
9254 if (!subr)
9255 for (i = 0; i < ntparms; i++)
9256 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
9257 {
9258 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9259
9260 /* If this is an undeduced nontype parameter that depends on
9261 a type parameter, try another pass; its type may have been
9262 deduced from a later argument than the one from which
9263 this parameter can be deduced. */
9264 if (TREE_CODE (tparm) == PARM_DECL
9265 && uses_template_parms (TREE_TYPE (tparm))
9266 && !saw_undeduced++)
9267 goto again;
9268
9269 if (!allow_incomplete)
9270 error ("incomplete type unification");
9271 return 2;
9272 }
9273 return 0;
9274 }
In this last part of type_unification_real above, at line 9254 subr if nonzero means type_unification_real is being called recursively, for which case, as we have seen, is the inner template parameter depends on outer one. But for the outmost template, all template parameters must be deduced.
If the undeduced one is a function parameter, consider below example:
template <class T> void func (int a[sizeof (T)], T t) {}
int main () {
int a[sizeof (int)];
func (a, int(5));
return 0;
}
In the first round deduction, the first parameter is undeduced dues to the unknown T at that deducing time. So it will satisfy the condition at line 9264, and start another round deduction via the goto statement at line 9267. It is different in that, targs now is added with the result of last deduction, as now T is known, the first parameter can be deduced this time.
If the deduction successes, the deduced arguments are placed within targs , then an instantiation of the template should be generated by instantiate_template below (recall that if the instantiation has been created the function would just return it).
add_template_candidate_real (continue)
2063 if (i != 0)
2064 return NULL;
2065
2066 fn = instantiate_template (tmpl, targs, tf_none);
2067 if (fn == error_mark_node)
2068 return NULL;
2069
2070 /* In [class.copy]:
2071
2072 A member function template is never instantiated to perform the
2073 copy of a class object to an object of its class type.
2074
2075 It's a little unclear what this means; the standard explicitly
2076 does allow a template to be used to copy a class. For example,
2077 in:
2078
2079 struct A {
2080 A(A&);
2081 template
2082 };
2083 const A f ();
2084 void g () { A a (f ()); }
2085
2086 the member template will be used to make the copy. The section
2087 quoted above appears in the paragraph that forbids constructors
2088 whose only parameter is (a possibly cv-qualified variant of) the
2089 class type, and a logical interpretation is that the intent was
2090 to forbid the instantiation of member templates which would then
2091 have that form. */
2092 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2093 {
2094 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2095 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2096 ctype))
2097 return NULL;
2098 }
2099
2100 if (obj != NULL_TREE)
2101 /* Aha, this is a conversion function. */
2102 cand = add_conv_candidate (candidates, fn, obj, access_path,
2103 conversion_path, arglist);
2104 else
2105 cand = add_function_candidate (candidates, fn, ctype,
2106 arglist, access_path,
2107 conversion_path, flags);
2108 if (DECL_TI_TEMPLATE (fn) != tmpl)
2109 /* This situation can occur if a member template of a template
2110 class is specialized. Then, instantiate_template might return
2111 an instantiation of the specialization, in which case the
2112 DECL_TI_TEMPLATE field will point at the original
2113 specialization. For example:
2114
2115 template
2116 template <> void f(int) {}; };
2117 S
2118 sd.f(3);
2119
2120 Here, TMPL will be template
2121 And, instantiate template will give us the specialization
2122 template <> S
2123 for this will point at template
2124 so that we can find the definition. For the purposes of
2125 overload resolution, however, we want the original TMPL. */
2126 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2127 else
2128 cand->template = DECL_TEMPLATE_INFO (fn);
2129
2130 return cand;
2131 }
In [3], page 207, further explains comment at line 2070 as:
“Because a template constructor is never a copy constructor, the presence of such a template does not suppress the implicit declaration of a copy constructor. Template constructors participate in overload resolution with other constructors, including copy constructors, and a template constructor may be used to copy an object if it provides a better match than other constructors. ”
Passing this check, the instantiated template function is the qualified candidate, obj at line 2100 is NULL indicating finding candidate according to type provided instead of object (so the focus of add_function_candidate is the specified fromtype and totype, while add_conv_candidate focuses on the specified object and totype; and here it is just finding function that converts given type, that is why only add_function_candidate will be invoked). Back to build_user_type_conversion_1 , it does similarly for conversion operators found.
build_user_type_conversion_1 (continue)
2420 if (convs)
2421 args = build_tree_list (NULL_TREE, build_this (expr));
2422
2423 for (; convs; convs = TREE_CHAIN (convs))
2424 {
2425 tree fns;
2426 tree conversion_path = TREE_PURPOSE (convs);
2427 int convflags = LOOKUP_NO_CONVERSION;
2428
2429 /* If we are called to convert to a reference type, we are trying to
2430 find an lvalue binding, so don't even consider temporaries. If
2431 we don't find an lvalue binding, the caller will try again to
2432 look for a temporary binding. */
2433 if (TREE_CODE (totype) == REFERENCE_TYPE)
2434 convflags |= LOOKUP_NO_TEMP_BIND;
2435
2436 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2437 {
2438 tree fn = OVL_CURRENT (fns);
2439
2440 /* [over.match.funcs] For conversion functions, the function
2441 is considered to be a member of the class of the implicit
2442 object argument for the purpose of defining the type of
2443 the implicit object parameter.
2444
2445 So we pass fromtype as CTYPE to add_*_candidate. */
2446
2447 if (TREE_CODE (fn) == TEMPLATE_DECL)
2448 cand = add_template_candidate (&candidates, fn, fromtype,
2449 NULL_TREE,
2450 args, totype,
2451 TYPE_BINFO (fromtype),
2452 conversion_path,
2453 flags,
2454 DEDUCE_CONV);
2455 else
2456 cand = add_function_candidate (&candidates, fn, fromtype,
2457 args,
2458 TYPE_BINFO (fromtype),
2459 conversion_path,
2460 flags);
2461
2462 if (cand)
2463 {
2464 tree ics = implicit_conversion (totype,
2465 TREE_TYPE (TREE_TYPE (cand->fn)),
2466 0, convflags);
2467
2468 cand->second_conv = ics;
2469
2470 if (ics == NULL_TREE)
2471 cand->viable = 0;
2472 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2473 cand->viable = -1;
2474 }
2475 }
2476 }
And unlike constructor acting as conversion function, conversion operator may return type that not the same as the destination type, but they must be matched by implicit_conversion at line 2464, this conversion is recorded as second_conv at line 2468 (compared with IDENTITY_CONV at line 2417 for constructor).