Using 'auto' on Private Types
In C , it may seem counterintuitive that one can utilize 'auto' with private types, as demonstrated in the following code:
1 2 3 4 5 |
|
Normally, it would be expected that attempting to access the private type 'Bar' directly (e.g., Foo::Bar b = f.Baz();) would result in an error. However, this is not the case when using 'auto':
1 2 3 4 5 |
|
This behavior arises due to the similarity between 'auto' rules and template type deduction. Similar to how private types can be passed to template functions:
1 2 3 4 5 6 7 |
|
This ability stems from the fact that while the name of a private type may be inaccessible, the type itself remains usable. Consequently, 'auto' is able to deduce the type correctly and assign it to the 'b' variable, despite being a private type.
The above is the detailed content of Can `auto` Deduce Private Types in C ?. For more information, please follow other related articles on the PHP Chinese website!