aboutsummaryrefslogtreecommitdiff
path: root/articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specia...
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-01-17 20:44:31 +0100
committerAdrian Kummerlaender2017-01-17 20:44:31 +0100
commit9bb990c9a663edc43aebb87ed84b00e6d90685c0 (patch)
treefcb40f5c4f94f7e1b89d3495a20d82e4cbd6db90 /articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md
parentb84e7973d91e066aa3c0e9e5660e30401916fd5f (diff)
downloadblog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.tar
blog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.tar.gz
blog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.tar.bz2
blog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.tar.lz
blog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.tar.xz
blog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.tar.zst
blog_content-9bb990c9a663edc43aebb87ed84b00e6d90685c0.zip
Update markdown syntax to use pandoc's peculiarities
Diffstat (limited to 'articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md')
-rw-r--r--articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md15
1 files changed, 6 insertions, 9 deletions
diff --git a/articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md b/articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md
index 98740c9..61983f3 100644
--- a/articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md
+++ b/articles/2014-01-05_disabling_methods_in_implicitly_instantiated_class_template_specializations_in_cpp.md
@@ -5,7 +5,7 @@ During my current undertaking of developing a [template based library](https://g
The specific scenario I am talking about is disabling the _serialize_ and _deserialize_ template methods in all specializations of the
_[Tuple](https://github.com/KnairdA/BinaryMapping/blob/master/src/tuple/tuple.h)_ class template whose _Endianess_ argument is not of the type _UndefinedEndian_. My first working implementation of this requirement looks as follows:
-~~~
+```cpp
template <
typename CustomOrder,
typename Helper = Endianess
@@ -16,8 +16,7 @@ inline typename std::enable_if<
>::type serialize() {
Serializer<InPlaceSorter<CustomOrder>>::serialize(this->tuple_);
}
-~~~
-{: .language-cpp}
+```
As we can see I am relying on the standard library's `std::enable_if` template to enable the method only if the _Helper_ template argument equals the type _UndefinedEndian_. One may wonder why I am supplying the `std::enable_if` template with the argument _Helper_ instead of directly specifying the class template argument _Endianess_. This was needed because of the following paragraph of the ISO C++ standard:
@@ -30,7 +29,7 @@ I defined a additional _Helper_ argument which defaults to the class template's
The code supplied above works as expected but has at least two flaws: There is an additonal template argument whose sole purpose is to work around the C++ standard to make _[SFINAE](https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error)_ possible and the return type of the method is obfuscated by the use of the `std::enable_if` template.
Luckily it is not the only way of achieving our goal:
-~~~
+```cpp
template <
typename CustomOrder,
typename = typename std::enable_if<
@@ -41,8 +40,7 @@ template <
inline void serialize() {
Serializer<InPlaceSorter<CustomOrder>>::serialize(this->tuple_);
}
-~~~
-{: .language-cpp}
+```
In this second example implementation we are moving the `std::enable_if` template from the return type into a unnamed default template argument of the method. This unnamed default
template argument which was not possible prior to the C++11 standard reduces the purpose of the `std::enable_if` template to selectively disabling method specializations. Additionally
@@ -50,7 +48,7 @@ the return type can be straight forwardly declared and the _Helper_ template arg
But during my research concerning this problem I came up with one additional way of achieving our goal which one could argue is even better than the second example:
-~~~
+```cpp
template <typename CustomOrder>
inline void serialize() {
static_assert(
@@ -60,8 +58,7 @@ inline void serialize() {
Serializer<InPlaceSorter<CustomOrder>>::serialize(this->tuple_);
}
-~~~
-{: .language-cpp}
+```
This implementation of the _serialize_ method renounces any additonal template arguments and instead uses a declaration called `static_assert` which makes any specializations where
the statement `std::is_same<Endianess, UndefinedEndian>::value` resolves to false ill-formed. Additionally it also returns a helpful message to the compiler log during such a situation.