Difference between revisions of "cpp/language/sizeof..."
From cppreference.com
(direct link) |
m (+tt in title) |
||
(21 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
− | {{title|sizeof... operator}} | + | {{title|{{tt|sizeof...}} operator {{ mark since c++11}}}} |
− | {{cpp/language/ | + | {{cpp/language/expressions/templates/navbar}} |
− | Queries the number of elements in a | + | Queries the number of elements in a {{rlp|parameter pack}}. |
===Syntax=== | ===Syntax=== | ||
+ | {{sdsc begin}} | ||
+ | {{sdsc|{{ttb|sizeof...(}} {{spar|parameter-pack}} {{ttb|)}}}} | ||
+ | {{sdsc end}} | ||
− | + | Returns a constant of type {{lc|std::size_t}}. | |
− | + | ||
− | + | ||
− | + | ||
− | Returns | + | |
===Explanation=== | ===Explanation=== | ||
− | + | Returns the number of elements in a {{rlp|parameter pack}}. | |
− | Returns the number of elements in a | + | |
===Keywords=== | ===Keywords=== | ||
− | + | {{ltt|cpp/keyword/sizeof}} | |
− | {{ltt|cpp/ | + | |
===Example=== | ===Example=== | ||
− | {{example | + | {{example |
− | + | |code= | |
+ | #include <array> | ||
#include <iostream> | #include <iostream> | ||
+ | #include <type_traits> | ||
− | template< | + | template<typename... Ts> |
− | + | constexpr auto make_array(Ts&&... ts) | |
{ | { | ||
− | return sizeof...( | + | using CT = std::common_type_t<Ts...>; |
+ | return std::array<CT, sizeof...(Ts)>{std::forward<CT>(ts)...}; | ||
} | } | ||
int main() | int main() | ||
{ | { | ||
− | std:: | + | std::array<double, 4ul> arr = make_array(1, 2.71f, 3.14, '*'); |
− | + | std::cout << "arr = { "; | |
− | + | for (auto s{arr.size()}; double elem : arr) | |
+ | std::cout << elem << (--s ? ", " : " "); | ||
+ | std::cout << "}\n"; | ||
} | } | ||
− | + | |output=arr = { 1, 2.71, 3.14, 42 } | |
− | + | ||
− | 1 | + | |
− | 3 | + | |
}} | }} | ||
===See also=== | ===See also=== | ||
− | * {{ | + | * {{rlpt|sizeof}} |
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 01:30, 14 August 2024
Queries the number of elements in a parameter pack.
Contents |
[edit] Syntax
sizeof...( parameter-pack )
|
|||||||||
Returns a constant of type std::size_t.
[edit] Explanation
Returns the number of elements in a parameter pack.
[edit] Keywords
[edit] Example
Run this code
#include <array> #include <iostream> #include <type_traits> template<typename... Ts> constexpr auto make_array(Ts&&... ts) { using CT = std::common_type_t<Ts...>; return std::array<CT, sizeof...(Ts)>{std::forward<CT>(ts)...}; } int main() { std::array<double, 4ul> arr = make_array(1, 2.71f, 3.14, '*'); std::cout << "arr = { "; for (auto s{arr.size()}; double elem : arr) std::cout << elem << (--s ? ", " : " "); std::cout << "}\n"; }
Output:
arr = { 1, 2.71, 3.14, 42 }