From 4bea2668c35f3983dd59682ecccba351e277f49c Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 5 Jan 2020 08:28:57 +1100 Subject: [PATCH] view: prefer static_assert over SFINAE for `extract` This produces more readable error messages, and we are not using `extract` in a context where we need to disambiguate calls anyway. --- view.hpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/view.hpp b/view.hpp index 445d9859..efc1bc9f 100644 --- a/view.hpp +++ b/view.hpp @@ -724,20 +724,24 @@ namespace cruft { /// It is assumed the user has taken care of alignment concerns template < typename ValueT, - typename WordT, - typename = std::enable_if_t< - // Only allow calls if the value is a multiple of the word size. - // It's useful to allow non-unit words for areas like TCP/IP which - // tend to include protocols that utilise u16 words. - sizeof (ValueT) % sizeof (WordT) == 0 && - // If WordT is const then ValueT must be const. Otherwise the user - // is free to choose. - std::is_const_v ? std::is_const_v : true - > + typename WordT > ValueT& extract (view &buffer) { + // Only allow calls if the value is a multiple of the word size. + // It's useful to allow non-unit words for areas like TCP/IP which + // tend to include protocols that utilise u16 words. + static_assert ( + sizeof (ValueT) % sizeof (WordT) == 0, + "The value type must be a multiple of the word size" + ); + + static_assert ( + !std::is_const_v or std::is_const_v, + "buffer and output types must have matching constness" + ); + if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT))) throw std::runtime_error ("insufficient data for extraction");