deduction guides for std::array

ぃ、小莉子 提交于 2021-02-05 06:16:45

问题


I go through the book C++ template unique quide and I try to understand how the deduction guides for std::array works. Regarding the definition of the standard the following is the declaration

template <class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;

For example if in main a array created as

std::array a{42,45,77} 

How the deduction takes place?

Thank you


回答1:


How the deduction takes place?

It's simple.

Calling

std::array a{42,45,77}

match

array(T, U...)

with T = decltype(42) and U... = decltype(45), decltype(77) that is T = int and U... = int, int.

So the type of a{42,45,47} become array<T, 1 + sizeof...(U)>, so std::array<int, 1 + sizeof...(int, int)>, so std::array<int, 1 + 2> that is std::array<int, 3>

In other words: are extracted the types of the arguments; the first one (T) is used to give the type the array (first template parameter); the others are used just to be counted (sizeof...(U)). But, for the template second parameter, it's important to count also the first argument (of type T, so the 1 in 1 + sizeof...(U)).



来源:https://stackoverflow.com/questions/50433139/deduction-guides-for-stdarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!