Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language
(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/sidebar}}
+
{{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|{{ttb|sizeof...(}} {{spar|parameter-pack}} {{ttb|)}}}}
 +
{{sdsc end}}
  
{{sdcl list begin}}
+
Returns a constant of type {{lc|std::size_t}}.
{{sdcl list item | {{ttb|sizeof...(}} {{sparam|parameter_pack}} {{ttb|)}}}}
+
{{sdcl list end}}
+
 
+
Returns an object of type {{cpp|std::size_t}}.
+
  
 
===Explanation===
 
===Explanation===
 
+
Returns the number of elements in a {{rlp|parameter pack}}.
Returns the number of elements in a [[cpp/language/parameter_pack|parameter pack]].
+
  
 
===Keywords===
 
===Keywords===
 
+
{{ltt|cpp/keyword/sizeof}}
{{ltt|cpp/keywords/sizeof}}
+
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
| code=
+
|code=
 +
#include <array>
 
#include <iostream>
 
#include <iostream>
 +
#include <type_traits>
  
template<class... Args>
+
template<typename... Ts>
std::size_t f()
+
constexpr auto make_array(Ts&&... ts)
 
{
 
{
     return sizeof...(Args);
+
    using CT = std::common_type_t<Ts...>;
 +
     return std::array<CT, sizeof...(Ts)>{std::forward<CT>(ts)...};
 
}
 
}
  
 
int main()
 
int main()
 
{
 
{
     std::cout << f<>() << '\n'
+
     std::array<double, 4ul> arr = make_array(1, 2.71f, 3.14, '*');
              << f<int>() << '\n'
+
    std::cout << "arr = { ";
              << f<char, int, double>() << '\n';
+
    for (auto s{arr.size()}; double elem : arr)
 +
        std::cout << elem << (--s ? ", " : " ");
 +
    std::cout << "}\n";
 
}
 
}
| output=
+
|output=arr = { 1, 2.71, 3.14, 42 }
0
+
1
+
3
+
 
}}
 
}}
  
 
===See also===
 
===See also===
* {{rlp|sizeof}}
+
* {{rlpt|sizeof}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

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