| Start |
|---|
| What is SFINAE SFINAE stands for Substitution Failure Is Not An Error. Ill-formed code that results from substituting types (or values) to instantiate a function template or a class template is not a hard compile error, it is only treated as a deduction failure. Deduction failures on instantiating function templates or class template specializations remove that candidate from the set of consideration - as if that failed candidate did not exist to begin with. Only substitution failures in the immediate context are considered deduction failures, all others are considered hard errors. |
| void_t
Some compilers require (opens new window) a slightly different implementation: The primary application of How does this work? When I try to instantiate
A simpler case: this doesn't use You may notice a common pattern of a void argument. We can factor this out: which hides the use of and: which seems simpler than the original versions. There are post-C++17 proposals for The utility of |
| enable_if
That is,
Here, a call to Now, instantiating When to use itIt's worth keeping in mind that Assuming that Now let's consider how we might want to implement arithmetic helpers that avoid signed integer overflow in favour of wrap around or modular behaviour. Which is to say that e.g. Once again which is the easiest to write, and which is the easiest to review and understand at a glance? A strength of |
| is_detected To generalize type_trait creation:based on SFINAE
there are experimental traits With template parameters
which can be implemented using Traits to detect presence of method can then be simply implemented: |
| Overload resolution with a large number of options If you need to select between several options,
enabling just one via The ordering between overloads can instead be selected using inheritance, i.e. tag dispatch. Instead of testing for the thing that needs to be well-formed, and also
testing the negation of all the other versions conditions, we instead test
just for what we need, preferably in a There are other methods commonly used to differentiate between overloads, such as exact match being better than conversion, being better than ellipsis. However, tag-dispatch can extend to any number of choices, and is a bit more clear in intent. |
| trailing decltype in function templates One of constraining function is to use trailing If I call If I call |
| enable_if_all / enable_if_any Motivational example When you have a variadic template pack in the template parameters list, like in the following code snippet: The standard library (prior to C++17) offers no direct way to write enable_if to impose SFINAE constraints on all of the parameters in If you do not have C++17 available, there are several solutions to achieve these. One of them is to use a base-case class and partial specializations, as demonstrated in answers of this question (opens new window). Alternatively, one may also implement by hand the behavior of Implementation of First let's emulate Then the implementation is quite straight-forward: Eventually some helpers: Usage The usage is also straight-forward: |
Read Article
https://develop.horje.com/learn/1434/reference