Namespaces
Variants
Actions

sizeof... operator (since C++11)

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Conditional execution statements
Iteration statements (loops)
Jump statements
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
Casts
Memory allocation
Class-specific function properties
Special member functions
Miscellaneous
 
Expressions
General
Literals
Operators
Conversions
 
 

Queries the number of elements in a pack.

[edit] Syntax

sizeof...( pack )

Returns a constant of type std::size_t .

[edit] Explanation

Returns the number of elements in a pack.

[edit] Keywords

sizeof

[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 }

[edit] See also

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/sizeof...&oldid=178138"

AltStyle によって変換されたページ (->オリジナル) /