Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language
(example: use variadic type instead of value)
m (Example: updated to make common_type_t be really useful; required C++20 now (although it can be converted back to C++11))
Line 19: Line 19:
 
{{example
 
{{example
 
  | code=
 
  | code=
#include <iostream>
 
 
#include <array>
 
#include <array>
 +
#include <iostream>
 
#include <type_traits>
 
#include <type_traits>
+
 
 
template<typename... Ts>
 
template<typename... Ts>
constexpr auto make_array(Ts&&... ts)
+
consteval auto make_array(Ts&&... ts)
    -> std::array<std::common_type_t<Ts...>,sizeof...(Ts)>
+
 
{
 
{
     return { std::forward<Ts>(ts)... };
+
    using CT = std::common_type_t<Ts...>;
 +
     return std::array<CT, sizeof...(Ts)>{ std::forward<CT>(ts)... };
 
}
 
}
+
 
 
int main()
 
int main()
 
{
 
{
     auto b = make_array(1, 2, 3);
+
     std::array<double, 4ul> arr = make_array(1, 2.71f, 3.14, '*');
     std::cout << b.size() << '\n';
+
     std::cout << "arr = { " << std::fixed;
     for (auto i : b)
+
     for (auto s{arr.size()}; double elem : arr)
         std::cout << i << ' ';
+
         std::cout << elem << (--s ? ", " : " ");
 +
    std::cout << "}\n";
 
}
 
}
  |output=
+
  |output=arr = { 1.000000, 2.710000, 3.140000, 42.000000 }
3
+
1 2 3
+
 
}}
 
}}
  

Revision as of 14:17, 17 August 2021

 
 
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

Syntax

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

Returns a constant of type std::size_t.

Explanation

Returns the number of elements in a parameter pack.

Keywords

sizeof

Example

#include <array>
#include <iostream>
#include <type_traits>
 
template<typename... Ts>
consteval 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 = { " << std::fixed;
    for (auto s{arr.size()}; double elem : arr)
        std::cout << elem << (--s ? ", " : " ");
    std::cout << "}\n";
}

Output:

arr = { 1.000000, 2.710000, 3.140000, 42.000000 }

See also