I read about deduction guides for std::vector
from using cppreference.
Example:
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::vector x{v.begin(), v.end()}; // uses explicit deduction guide
}
So, I have some questions about it:
What are
std::vector
deduction guides in C++17?Why and when do we need vector deduction?
Here, Is
x
astd::vector<int>
or astd::vector<std::vector<int>>
?
An user-defined deduction guide allows users to decide how class template argument deduction deduces arguments for a template class from its constructor arguments. In this case, it seems that
std::vector
has an explicit guide that should make construction from an iterator pair more intuitive.We don't "need" it, but it is useful in generic code and in code that's very obvious (i.e. code where explicitly specifying the template arguments is not beneficial to the reader).
Here's a nice trick to figure this out quickly - write a template function declaration without a definition and attempt to call it. The compiler will print out the type of the passed arguments. Here's what g++ 8 prints out:
As you can see from the error message,
x
is deduced tostd::vector<std::vector<int>::iterator>
.std::vector
's deduction guides are available on cppreference.org. The Standard seems to define an explicit deduction guide from an iterator pair:The behavior encountered in g++ 8 seems to be correct regardless, as (quoting Rakete1111)
std:vector<std::vector<int>::iterator>
is therefore the correct result when using list-initialization. live exampleWhen constructing
x
withstd::vector x(v.begin(), v.end())
,int
will be deduced instead. live example