Template argument deduction failed


















If only one succeeds, that successful deduction is used. If none or more than one succeeds, the template parameter is non-deduced context see below :. Before deduction begins, the following adjustments to P and A are made:. After these transformations, the deduction processes as described below cf. If the usual deduction from P and A fails, the following alternatives are additionally considered:.

In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified.

If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails. In any case, if any part of a type name is non-deduced, the entire type name is non-deduced context.

However, compound types can include both deduced and non-deduced type names. Given a function parameter P that depends on one or more type template parameters T i, template template parameters TT i, or non-type template parameters I i, and the corresponding argument A , deduction takes place if P has one of the following forms:. If the last P i is a pack expansion, then its pattern is compared against each remaining argument in the template argument list of A.

A trailing parameter pack that is not otherwise deduced, is deduced to an empty parameter pack. If P has one of the forms that include a function parameter list T , then each parameter P i from that list is compared with the corresponding argument A i from A 's function parameter list. If the last P i is a pack expansion, then its declarator is compared with each remaining A i in the parameter type list of A.

When the value of the argument corresponding to a non-type template parameter P that is declared with a dependent type is deduced from an expression, the template parameters in the type of P are deduced from the type of the value. If a non-type template parameter of function template is used in the template parameter list of function parameter which is also a template , and the corresponding template argument is deduced, the type of the deduced template argument as specified in its enclosing template parameter list, meaning references are preserved must match the type of the non-type template parameter exactly, except that cv-qualifiers are dropped, and except where the template argument is deduced from an array bound—in that case any integral type is allowed, even bool though it would always become true:.

Deduction of template template parameter can use the type used in the template specialization used in the function call:. Such feature has not been accepted by the standard. This is CWG issue Besides function calls and operator expressions, template argument deduction is used in the following situations:. Template argument deduction is used in declarations of variables, when deducing the meaning of the auto specifier from the variable's initializer.

The argument A is the initializer expression. After deduction of U from P and A following the rules described above, the deduced U is substituted into P to get the actual variable type:. In direct-list-initialization but not in copy-list-initalization , when deducing the meaning of the auto from a braced-init-list, the braced-init-list must contain only one element, and the type of auto will be the type of that element:. Template argument deduction is used in declarations of functions , when deducing the meaning of the auto specifier in the function's return type, from the return statement.

For auto-returning functions, the parameter P is obtained as follows: in T , the declared return type of the function that includes auto , every occurrence of auto is replaced with an imaginary type template parameter U. The argument A is the expression of the return statement, and if the return statement has no operand, A is void. After deduction of U from P and A following the rules described above, the deduced U is substituted into T to get the actual return type:.

If such function has multiple return statements, the deduction is performed for each return statement. All the resulting types must be the same and become the actual return type. If such function has no return statement, A is void when deducing. Note: the meaning of decltype auto placeholder in variable and function declarations does not use template argument deduction. Template argument deduction is used during overload resolution , when generating specializations from a candidate template function.

P and A are the same as in a regular function call:. If deduction fails, or if deduction succeeds, but the specialization it produces would be invalid for example, an overloaded operator whose parameters are neither class nor enumeration types , the specialization is not included in the overload set, similar to SFINAE.

Template argument deduction is used when taking an address of a overload set , which includes function templates. The function type of the function template is P. The target type is the type of A :. If the return type of the function template is a placeholder auto or decltype auto , that return type is a non-deduced context and is determined from the instantiation. Template argument deduction is used during partial ordering of overloaded function templates.

Template argument deduction is used when selecting user-defined conversion function template arguments. A is the type that is required as the result of the conversion.

P is the return type of the conversion function template, except that. If A is cv-qualified, the top-level cv-qualifiers are ignored. Here are two of the most common cases for deduction guides in the STL: iterators and perfect forwarding. That essentially dereferences the iterator type to get the element type we want. The other case is perfect forwarding, where MyAdvancedPair has a perfect forwarding constructor like std::pair does. Such deduction performs decay. This demonstrates a critical fact about CTAD and deduction guides.

That deduction either succeeds determining a unique type or fails. Once the type to construct has been chosen, overload resolution to determine which constructor to call happens normally. Afterwards, overload resolution chooses the perfect forwarding constructor, which takes its arguments by perfect forwarding, exactly as if the class type had been written with explicit template arguments. CTAD and deduction guides are also non-intrusive. In rare cases, you might want deduction guides to reject certain code.

Like std::array , MyArray is an aggregate with no actual constructors, but CTAD still works for these class templates via deduction guides. Similar techniques could be used to make CTAD entirely ill-formed for certain constructors, or all constructors. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. Template argument deduction failed Ask Question. Asked 9 years, 1 month ago. Active 9 years, 1 month ago. Viewed times. Improve this question. Add a comment. Active Oldest Votes. Improve this answer. AnT AnT k 39 39 gold badges silver badges bronze badges. Unfortunately, the latest draft here open-std. But i take your word for it : — keveman.



0コメント

  • 1000 / 1000