Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is copy deduction candidate necessary as a separate deduction guide?

Tags:

People also ask

What is a deduction guide?

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.

What is template argument deduction?

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.