From c698d80a809b62f20c8910b2f06f0f8f8d0001a4 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 26 Apr 2015 02:35:34 +0200 Subject: [PATCH] build in a catch-all to signal failure this overload will be picked only if none of the more specific overloads is applicable. Instantiating this overload will then trigger a static assertion failure. This way we sort out impossible or dangerous combinations at compile time already. I found no simple way to include the actual type parameters in the generated error message (string concatenation at compiletime) The throw-statement is only there to prevent a warning due to missing return statement. --- src/lib/access-casted.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/access-casted.hpp b/src/lib/access-casted.hpp index eff197e4b..b4ec5462f 100644 --- a/src/lib/access-casted.hpp +++ b/src/lib/access-casted.hpp @@ -162,8 +162,21 @@ namespace util { return AccessCasted::access (*elem); } + + + /** catch-all to signal failure of conversion */ + static TAR + access (...) + { + // NOTE: if you see this assertion failure, none of the above predicates were true. + // Chances are that you requested a conversion that is logically impossible or dangerous, + // like e.g. taking a reference from an anonymous value parameter + static_assert (!sizeof(TAR), "AccessCasted: No valid conversion or cast supported for these types."); + throw error::Invalid("impossible or unsafe type conversion requested"); + } }; + } // namespace util #endif