sizeof...
operator
(since C++11)
From cppreference.net
C++
C++ language
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| 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 | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
Expressions
| General | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
| Literals | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
| Operators | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
| Conversions | |||||||||||||||||||||||||||||||||||||||||||||||||||
Templates
| Template parameters | ||||
| Template arguments | ||||
| Class templates | ||||
| Function templates | ||||
| Class member templates | ||||
| Variable templates (C++14) | ||||
| Template argument deduction | ||||
| Class template argument deduction (C++17) | ||||
| Explicit (full) specialization | ||||
| Partial specialization | ||||
| Dependent names | ||||
| Packs (C++11) | ||||
| sizeof... (C++11) | ||||
| Fold expressions (C++17) | ||||
| Pack indexing (C++26) | ||||
| SFINAE | ||||
| Constraints and concepts (C++20) | ||||
| requires expression (C++20) |
查询 包 中的元素数量。
目录 |
语法
sizeof...(
参数包
)
|
|||||||||
返回一个类型为 std::size_t 的常量。
说明
返回 参数包 中的元素数量。
关键词
示例
运行此代码
#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"; }
输出:
arr = { 1, 2.71, 3.14, 42 }