Template deduction guides are patterns associated with a template class that tell the compiler how to translate a set of constructor arguments (and their types) into template parameters for the class. The simplest example is that of std::vector and its constructor that takes an iterator pair.
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.
template <typename T> struct A {
A(T);
A(const A&);
};
int main()
{
A x(42); // #1
A y = x; // #2
}
As I understand,T
for #1 will be deduced using the implicit deduction guide
generated from the first ctor. Then x
will be initialized using that ctor.
For #2 though, T
will be deduced using the copy deduction candidate (which, as I understand, is a specific case of a deduction guide) (and then y
will be initialized using the 2nd ctor).
Why couldn't T
for #2 be deduced using the (implicit) deduction guide generated from the copy-ctor?
I guess I just don't understand the general purpose of the copy deduction candidate.
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