You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In C++23, you can declare the function call operator (`operator()`) as a static member function. A static function call operator doesn't have an implicit object parameter. Use it when a callable type doesn't need to access instance data.
11
+
In C++23, you can declare the function call operator, `operator()`, as a static member function. A static function call operator doesn't have an implicit object parameter. Use it when a callable type doesn't need to access instance data.
12
12
13
-
Support for this feature was introduced in Visual Studio 2022 version 17.14 (MSVC 14.44). Use the `/std:c++latest` compiler option.
13
+
Support for this feature was introduced in Visual Studio 2022 version 17.14 (MSVC 14.44). Use the `/std:c++latest`or `/std:c++23preview`compiler option.
14
14
15
15
## Syntax
16
16
@@ -22,6 +22,7 @@ You can also declare the function call operator generated for a lambda expressio
22
22
23
23
```cpp
24
24
[](parameter-list) static { function-body }
25
+
[] static { function-body }
25
26
```
26
27
27
28
## Remarks
@@ -30,13 +31,13 @@ A static function call operator doesn't have a `this` pointer. It can't be `virt
30
31
31
32
You can call a static function call operator by using an object of its class, which allows the object to work as a function object. You can also call it by using its qualified name. Taking its address produces a regular function pointer instead of a pointer-to-member function.
32
33
33
-
A lambda expression can specify `static` after its parameter list. A static lambda can't have captures or be declared `mutable`. Declaring a captureless lambda doesn't make it static automatically; you must specify `static` to opt in to this behavior.
34
+
A lambda expression can specify `static` after its parameter list. When the lambda has no parameters, you can omit the empty parameter list and specify `static` after the lambda introducer (`[]`). A static lambda can't have captures or be declared `mutable`. Declaring a captureless lambda doesn't make it static automatically; you must specify `static` to opt in to this behavior.
34
35
35
36
The feature-test macro `__cpp_static_call_operator` is defined when the static function call operator is available.
36
37
37
-
## Example
38
+
## Static function call operator example
38
39
39
-
The following example defines a stateless function object and calls its static function call operator in three ways:
40
+
The following example defines a stateless function object and calls its static function call operator in three ways. It also defines a static lambda expression and calls it:
40
41
41
42
```cpp
42
43
// Compile with: /std:c++latest
@@ -63,6 +64,7 @@ int main()
63
64
std::cout << "multiply_function(5, 5) = "
64
65
<< multiply_function(5, 5) << std::endl;
65
66
67
+
// A static lambda expression that doubles its argument
In C++23, you can declare the subscript operator (`operator[]`) as a static member function. A static subscript operator doesn't have an implicit object parameter. Use it when a subscript operation doesn't need to access instance data.
11
+
In C++23, you can declare the subscript operator, `operator[]`, as a static member function. A static subscript operator doesn't have an implicit object parameter. Use it when a subscript operation doesn't need to access instance data.
12
12
13
13
Support for this feature was introduced in Visual Studio 2022 version 17.14 (MSVC 14.44). Use the `/std:c++latest` compiler option.
A static subscript operator doesn't have a `this` pointer. It can't be `virtual` or have a cv-qualifier (`const` or `volatile`) or ref-qualifier (`&`, `&&`).
24
24
25
-
You can call a static subscript operator by using an object of its class, which allows the object to use subscript syntax. You can also call it by using its qualified name. Taking its address produces a regular function pointer instead of a pointer-to-member function.
25
+
You can call a static subscript operator through an object or by using its qualified name. Taking its address produces a regular function pointer instead of a pointer-to-member function.
26
26
27
-
The feature-test macro `__cpp_static_call_operator` is defined when the static subscript operator is available.
27
+
The feature-test macro `__cpp_multidimensional_subscript` has a value of at least `202211L`when the static subscript operator is available. Simply checking whether the macro is defined is insufficient because its earlier value of `202110L` covers multidimensional subscripting but not `static operator[]`:
The subscript operator (**[]**), like the function-call operator, is a binary operator. Before C++23, the subscript operator must be a nonstatic member function. In C++23 and later, it can be a static member function. For more information, see [Static subscript operator](static-subscript-operator.md).
10
+
The built-in subscript operator (**[]**)is a binary operator. Before C++23, you had to overload the subscript operator (`operator[]`) by using a nonstatic subscript operator member function. In C++23 and later, you can also overload it by using a static subscript operator member function. For more information, see [Static subscript operator](static-subscript-operator.md).
11
11
12
12
The argument can be any type and designates the desired array subscript.
0 commit comments