Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does providing an explicit deduction guide disable the generation/formation of implicit deduction guides

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.

like image 709
Anoop Rana Avatar asked Sep 15 '25 22:09

Anoop Rana


1 Answers

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 of C, 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 constructor C().
  • 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.

like image 177
Brian Bi Avatar answered Sep 18 '25 12:09

Brian Bi