Difference between revisions of "cpp/language/sizeof..."
From cppreference.com
m (→Example: updated to make common_type_t be really useful; required C++20 now (although it can be converted back to C++11)) |
m (+tt in title) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {{title|sizeof... operator}} | + | {{title|{{tt|sizeof...}} operator {{ mark since c++11}}}} |
− | {{cpp/language/templates/navbar}} | + | {{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 begin}} | ||
− | {{sdsc | {{ttb|sizeof...(}} {{spar| | + | {{sdsc|{{ttb|sizeof...(}} {{spar|parameter-pack}} {{ttb|)}}}} |
{{sdsc end}} | {{sdsc end}} | ||
Line 11: | Line 11: | ||
===Explanation=== | ===Explanation=== | ||
− | Returns the number of elements in a | + | Returns the number of elements in a {{rlp|parameter pack}}. |
===Keywords=== | ===Keywords=== | ||
Line 18: | Line 18: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
#include <array> | #include <array> | ||
#include <iostream> | #include <iostream> | ||
Line 24: | Line 24: | ||
template<typename... Ts> | template<typename... Ts> | ||
− | + | constexpr auto make_array(Ts&&... ts) | |
{ | { | ||
using CT = std::common_type_t<Ts...>; | using CT = std::common_type_t<Ts...>; | ||
− | return std::array<CT, sizeof...(Ts)>{ std::forward<CT>(ts)... }; | + | return std::array<CT, sizeof...(Ts)>{std::forward<CT>(ts)...}; |
} | } | ||
Line 33: | Line 33: | ||
{ | { | ||
std::array<double, 4ul> arr = make_array(1, 2.71f, 3.14, '*'); | std::array<double, 4ul> arr = make_array(1, 2.71f, 3.14, '*'); | ||
− | std::cout << "arr = { " | + | std::cout << "arr = { "; |
for (auto s{arr.size()}; double elem : arr) | for (auto s{arr.size()}; double elem : arr) | ||
std::cout << elem << (--s ? ", " : " "); | std::cout << elem << (--s ? ", " : " "); | ||
std::cout << "}\n"; | std::cout << "}\n"; | ||
} | } | ||
− | + | |output=arr = { 1, 2.71, 3.14, 42 } | |
}} | }} | ||
===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 }