Skip to content

Commit 01ab3b9

Browse files
committed
Add example for explicit conversion operators
1 parent 51ca655 commit 01ab3b9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

03-Style.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,23 @@ Instead mark single parameter constructors as `explicit`, which requires them to
349349

350350
Similarly to single parameter constructors, conversion operators can be called by the compiler and introduce unexpected overhead. They should also be marked as `explicit`.
351351

352+
```cpp
353+
//bad idea
354+
struct S {
355+
operator int() {
356+
return 2;
357+
}
358+
};
359+
```
360+
361+
```cpp
362+
//good idea
363+
struct S {
364+
explicit operator int() {
365+
return 2;
366+
}
367+
};
368+
```
352369

353370
## Consider the Rule of Zero
354371

0 commit comments

Comments
 (0)