From 01ab3b9fa5f43097682f741a7daeae95586ef759 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 24 May 2016 22:20:03 -0600 Subject: [PATCH] Add example for explicit conversion operators --- 03-Style.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/03-Style.md b/03-Style.md index adaaff8..591dadb 100644 --- a/03-Style.md +++ b/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