包索引 (C++26 起)
來自 cppreference.com
訪問指定索引處的包元素。
目錄 |
[編輯] 語法
id-expression ...[ expression ] |
(1) | ||||||||
typedef-name ...[ expression ] |
(2) | ||||||||
1) 包索引表示式
2) 包索引說明符
typedef-name | - | 命名包的識別符號或簡單模板 ID |
id-expression | - | 命名包的id-expression |
表示式 | - | 一個轉換後的常量表達式 I,型別為 std::size_t,指定為索引,其中 I 在包索引中某個包 P 的範圍 [ 0, sizeof...(P)) 內 |
[編輯] 解釋
包索引是未展開包後跟省略號和下標內索引的*包擴充套件*。包索引有兩種:包索引表示式和包索引說明符。
設 P
是一個非空包,包含 P0, P1, ..., Pn-1
,且 I
是一個有效索引,則擴充套件 P...[I]
的例項化產生 P
的包元素 PI
。
不允許使用非常量表達式索引 I
對包進行索引。
int runtime_idx(); void bar(auto... args) { auto a = args...[0]; const int n = 1; auto b = args...[n]; int m = 2; auto c = args...[m]; // error: 'm' is not a constant expression auto d = args...[runtime_idx()]; // error: 'runtime_idx()' is not a constant expression }
不能對模板模板引數包進行索引。
template <template <typename...> typename... Temps> using A = Temps...[0]<>; // error: 'Temps' is a pack of template template parameters template <template <typename...> typename... Temps> using B = Temps<>...[0]; // error: 'Temps<>' doesn't denote pack name // although it is a simple-template-id
[編輯] 包索引表示式
id-expression ...[ expression ] |
|||||||||
包索引表示式表示*id-expression*,即包元素 PI
的表示式。該id-expression應由以下宣告引入:
template <std::size_t I, typename... Ts> constexpr auto element_at(Ts... args) { // 'args' introduced in function parameter pack declaration return args...[I]; } static_assert(element_at<0>(3, 5, 9) == 3); static_assert(element_at<2>(3, 5, 9) == 9); static_assert(element_at<3>(3, 5, 9) == 4); // error: out of bounds static_assert(element_at<0>() == 1); // error: out of bounds, empty pack template <std::size_t I, typename Tup> constexpr auto structured_binding_element_at(Tup tup) { auto [...elems] = tup; // 'elems' introduced in structured binding pack declaration return elems...[I]; } struct A { bool a; int b; }; static_assert(structured_binding_element_at<0>(A {true, 4}) == true); static_assert(structured_binding_element_at<1>(A {true, 4}) == 4); // 'Vals' introduced in non-type template parameter pack declaration template <std::size_t I, std::size_t... Vals> constexpr std::size_t double_at = Vals...[I] * 2; // OK template <std::size_t I, typename... Args> constexpr auto foo(Args... args) { return [...members = args](Args...[I] op) { // 'members' introduced in lambda init-capture pack return members...[I] + op; }; } static_assert(foo<0>(4, "Hello", true)(5) == 9); static_assert(foo<1>(3, std::string("C++"))("26") == "C++26");
不允許對除 id-expression 之外的複雜表示式包進行索引。
template <std::size_t I, auto... Vals> constexpr auto identity_at = (Vals)...[I]; // error // use 'Vals...[I]' instead template <std::size_t I, std::size_t... Vals> constexpr std::size_t triple_at = (Vals * 3)...[I]; // error // use 'Vals...[I] * 3' instead template <std::size_t I, typename... Args> constexpr decltype(auto) get(Args&&... args) noexcept { return std::forward<Args>(args)...[I]; // error // use 'std::forward<Args...[I]>(args...[I])' instead }
將decltype
應用於包索引表示式與將decltype
應用於 id-expression 相同。
void f() { [](auto... args) { using T0 = decltype(args...[0]); // 'T0' is 'double' using T1 = decltype((args...[0])); // 'T1' is 'double&' }(3.14); }
[編輯] 包索引說明符
typedef-name ...[ expression ] |
|||||||||
包索引說明符表示*計算型別說明符*,即包元素 PI
的型別。該typedef-name應由型別模板引數包的宣告引入。
template <typename... Ts> using last_type_t = Ts...[sizeof...(Ts) - 1]; static_assert(std::is_same_v<last_type_t<>, int>); // error: out of bounds static_assert(std::is_same_v<last_type_t<int>, int>); static_assert(std::is_same_v<last_type_t<bool, char>, char>); static_assert(std::is_same_v<last_type_t<float, int, bool*>, bool*>);
包索引說明符可以作為以下形式出現:
- 一個簡單型別說明符,
- 一個基類說明符,
- 一個巢狀名稱說明符,或
- 顯式解構函式呼叫的型別。
包索引說明符可用於函式或建構函式引數列表,以在模板引數推導中建立非推導上下文。
template <typename...> struct type_seq {}; template <typename... Ts> auto f(Ts...[0] arg, type_seq<Ts...>) { return arg; } // OK: "Hello" is implicitly converted to 'std::string_view' std::same_as<std::string_view> auto a = f("Hello", type_seq<std::string_view>{}); // Error: "Ok" is not convertible to 'int' std::same_as<int> auto b = f("Ok", type_seq<int, const char*>{});
[編輯] 注意
在 C++26 之前,Ts...[N] 是宣告大小為 N 的未命名陣列的函式引數包的有效語法,其中引數型別會進一步調整為指標。從 C++26 開始,Ts...[1] 被解釋為包索引說明符,這將把下面的行為改為 #2。為了保留第一種行為,函式引數包必須命名,或手動調整為指標型別包。
template <typename... Ts> void f(Ts... [1]); template <typename... Ts> void g(Ts... args[1]); template <typename... Ts> void h(Ts*...); // clearer but more permissive: Ts... can contain cv void or function types void foo() { f<char, bool>(nullptr, nullptr); // behavior #1 (before C++26): // calls void 'f<char, bool>(char*, bool*)' (aka 'f<char, bool>(char[1], bool[1])') // behavior #2 (since C++26): // error: supposedly called 'void f<char, bool>(bool)' // but provided with 2 arguments instead of 1 g<char, bool>(nullptr, nullptr); // calls 'g<char, bool>(char*, bool*)' (aka 'g<char, bool>(char[1], bool[1])') h<char, bool>(nullptr, nullptr); // calls 'h<char, bool>(char*, bool*)' }
功能測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_pack_indexing |
202311L |
(C++26) | 包索引 |
[編輯] 示例
執行此程式碼
#include <tuple> template <std::size_t... Indices, typename Decomposable> constexpr auto splice(Decomposable d) { auto [...elems] = d; return std::make_tuple(elems...[Indices]...); } struct Point { int x; int y; int z; }; int main() { constexpr Point p { .x = 1, .y = 4, .z = 3 }; static_assert(splice<2, 1, 0>(p) == std::make_tuple(3, 4, 1)); static_assert(splice<1, 1, 0, 0>(p) == std::make_tuple(4, 4, 1, 1)); }