mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 03:04:36 +03:00
Add example for explicit conversion operators
This commit is contained in:
17
03-Style.md
17
03-Style.md
@@ -349,6 +349,23 @@ Instead mark single parameter constructors as `explicit`, which requires them to
|
||||
|
||||
Similarly to single parameter constructors, conversion operators can be called by the compiler and introduce unexpected overhead. They should also be marked as `explicit`.
|
||||
|
||||
```cpp
|
||||
//bad idea
|
||||
struct S {
|
||||
operator int() {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```cpp
|
||||
//good idea
|
||||
struct S {
|
||||
explicit operator int() {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## Consider the Rule of Zero
|
||||
|
||||
|
||||
Reference in New Issue
Block a user