Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
44
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

318

Chapter 9: Spans

auto s1 = spDyn.subspan(2); auto s2 = spDyn.subspan(2, 2);

auto s3 = spDyn.subspan(2, std::dynamic_extent);

std::array arr{1.1, 2.2, 3.3, 4.4, 5.5}; std::span spFix{arr};

auto s4 = spFix.subspan(2); auto s5 = spFix.subspan(2, 2);

auto s6 = spFix.subspan(2, std::dynamic_extent);

//3rd to last elem with dynamic extent

//3rd to 4th elem with dynamic extent

//3rd to last with dynamic extent

//3rd to last elem with dynamic extent

//3rd to 4th elem with dynamic extent

//3rd to last with dynamic extent

However, when passing template parameters, the result might be different than what you expect:

std::vector vec{1.1, 2.2, 3.3, 4.4, 5.5}; std::span spDyn{vec};

auto s1 =

spDyn.subspan<2>();

// 3rd to last with dynamic extent

auto s2 =

spDyn.subspan<2, 2>();

// 3rd to 4th with fixed extent

auto s3

=

spDyn.subspan<2, std::dynamic_extent>(); // 3rd to last with dynamic extent

std::array arr{1.1, 2.2, 3.3, 4.4, 5.5};

 

std::span spFix{arr};

 

auto s4

= spFix.subspan<2>();

// 3rd to last with fixed extent

auto s5

= spFix.subspan<2, 2>();

// 3rd to 4th with fixed extent

auto s6

= spFix.subspan<2, std::dynamic_extent>(); // 3rd to last with fixed extent

9.5Afternotes

Spans were motivated as array_views by Lukasz Mendakiewicz and Herb Sutter in http://wg21.link/ n3851 and first proposed by Neil MacIntosh in http://wg21.link/p0122r0. The finally accepted wording was formulated by Neil MacIntosh and Stephan T. Lavavej in http://wg21.link/p0122r7.

Several modifications were added later on, such as removing all comparison operators as proposed by Tony Van Eerd in http://wg21.link/P1085R2, and removing const_iterator support with the resolution of http://wg21.link/lwg3320.

After C++20 was released, the definition of views changed so that spans are now always views (see http://wg21.link/p2325r3).