Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/sizeof..."

From cppreference.com
< cpp‎ | language
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 [[cpp/language/parameter_pack|parameter pack]].
+
Queries the number of elements in a {{rlp|parameter pack}}.
  
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc | {{ttb|sizeof...(}} {{spar|parameter_pack}} {{ttb|)}} | notes={{mark since c++11}}}}
+
{{sdsc|{{ttb|sizeof...(}} {{spar|parameter-pack}} {{ttb|)}}}}
 
{{sdsc end}}
 
{{sdsc end}}
  
Line 11: Line 11:
  
 
===Explanation===
 
===Explanation===
Returns the number of elements in a [[cpp/language/parameter_pack|parameter pack]].
+
Returns the number of elements in a {{rlp|parameter pack}}.
  
 
===Keywords===
 
===Keywords===
Line 18: Line 18:
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
 
#include <array>
 
#include <array>
 
#include <iostream>
 
#include <iostream>
Line 24: Line 24:
  
 
template<typename... Ts>
 
template<typename... Ts>
consteval auto make_array(Ts&&... 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::fixed;
+
     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.000000, 2.710000, 3.140000, 42.000000 }
+
|output=arr = { 1, 2.71, 3.14, 42 }
 
}}
 
}}
  
 
===See also===
 
===See also===
* {{rlp|sizeof}}
+
* {{rlpt|sizeof}}
  
[[de:cpp/language/sizeof...]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/language/sizeof...]]
+
[[fr:cpp/language/sizeof...]]
+
[[it:cpp/language/sizeof...]]
+
[[ja:cpp/language/sizeof...]]
+
[[pt:cpp/language/sizeof...]]
+
[[ru:cpp/language/sizeof...]]
+
[[zh:cpp/language/sizeof...]]
+

Latest revision as of 01:30, 14 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 
 

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

sizeof

[edit] Example

#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