I am reading about deduction guides in C++17. So say we have the following example:
template<typename T> struct Custom
{
};
template<typename T> struct Person
{
Person(Custom<T> const&);
Person(Custom<T>&&);
};
template<typename T> Person(Custom<T>) -> Person<T>; //explicitly declared deduction guide
My question is that will the explicit declaration for the explicit deduction guide(as shown above) disable the formation of the 2 implicit deduction guides(corresponding to the 2 ctors of class template Person
) that would have been there if we did not specify an explicit deduction guide. I mean, suppose in the above example, there was no explicit deduction guide, then there will be 2 implicit deduction guides(corresponding to the two constructors of Person
). But after we have added explicit guide, will we have a total of 1 deduction guide(explicitly declared by us the user) or 3 deduction guides(including 2 implicit deduction guides).
PS: Note that the question is only about the deduction guides for Person
and not about whether implicit guide will be formed for Custom
.
No, deduction-guide declarations do not suppress the formation of implicit deduction guides during the class template argument deduction process. The overload set will contain both. See [over.match.class.deduct]/1:
... a set of functions and function templates, called the guides of
C
, is formed comprising:
- If
C
is defined, for each constructor ofC
, a function template with the following properties: ...- If
C
is not defined or does not declare any constructors, an additional function template derived as above from a hypothetical constructorC()
.- An additional function template derived as above from a hypothetical constructor
C(C)
, called the copy deduction candidate.- For each deduction-guide, a function or function template with the following properties: ...
[additional provisions related to aggregates omitted...]
However, the third last tiebreaker in the overload resolution process gives precedence to deduction-guides (i.e., explicitly declared deduction guides) over implicit deduction guides. [over.match.best.general]/2.10. This tiebreaker is only reached after all of the more general tiebreakers (not related directly to deduction guides) are tried.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With