From 6c55d4eaafac603ea9a5770e530bd970fea34c59 Mon Sep 17 00:00:00 2001 From: Ari Hietanen Date: Mon, 26 Feb 2018 21:11:51 +0200 Subject: [PATCH] Bug fix C4 class Foobar (#1143) * Fixed bug in example code of C4 Expecptions. In C4, the class function void Foobar::foo(double x) is supposed to call the overloaded void Foobar::foo(int x), but in the call foo(std::round(x)), std::round returns a double. Hence, it will get stuck in an infinite recursive loop. Added static_cast(..) to enforce the call to right overload. Added also keyword public to be more consistent. * Changed static_cast to narrow_cast following ES.46. * Modified C4 Foobar class, s.t, std::lround(x) is now called in void foo(double) and the overload is changed to void foo(long) from (int). Now there is no need for conversions. --- CppCoreGuidelines.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index e672ba2..0ff59f1 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -4081,8 +4081,9 @@ The language requires operators `=`, `()`, `[]`, and `->` to be members. An overload set may have some members that do not directly access `private` data: class Foobar { - void foo(int x) { /* manipulate private data */ } - void foo(double x) { foo(std::round(x)); } + public: + void foo(long x) { /* manipulate private data */ } + void foo(double x) { foo(std::lround(x)); } // ... private: // ...