|
LibSWOC++ 1.5.14
Solid Wall of C++
|
Case hierarchy. More...
#include <swoc_meta.h>

Static Public Attributes | |
| static constexpr unsigned | value = N |
| Case tag value. | |
Case hierarchy.
This creates an ordered series of meta template cases that can be used to select one of a set of functions in a priority ordering. A set of templated overloads take an (extra) argument of the case structures, each a different one. Calling the function invokes the highest case that is valid. Because of SFINAE the templates can have errors, as long as at least one doesn't. The root technique is to use decltype to check an expression for the overload to be valid. Because the compiler will evaluate everything it can while parsing the template this expression must be delayed until the template is instantiated. This is done by making the return type auto and making the decltype dependent on the template parameter. In addition, the comma operator can be used to force a specific return type while also checking the expression for validity. E.g.
The comma operator discards the type and value of the left operand therefore the return type of the function is int but this overload will not be available if t.item does not compile (e.g., there is no such member). The presence of t.item also prevents this compilation check from happening until overload selection is needed. Therefore if the goal was a function that would return the value of the T::count member if present and 0 if not, the code would be
Note the overloads will be checked from the highest case to the lowest and the first one that is valid (via SFINAE) is used. This is the point of using the case arguments, to force an ordering on the overload selection. Unfortunately this means the functions must be templated, even if there's no other reason for it, because it depends on SFINAE which doesn't apply to normal overloads.
The key point is the expression in the decltype should be the same expression used in the method to verify it will compile. It is annoying to type it twice but there's not a better option.
Note decltype does not accept explicit types - to have the type of "int" a function returning int must be provided. This is easy for simple builtin types such as int - use the constructor int(). For void and non-simple types (such as int* ) this is a bit more challenging. A general utility is provided for this - TypeFunc. For the void case this would be decltype(TypeFunc<void>()). For int* it would be decltype(TypeFunc<int *>()).
Definition at line 66 of file swoc_meta.h.
|
inlineconstexpr |
Definition at line 67 of file swoc_meta.h.
|
staticconstexpr |
Case tag value.
Definition at line 69 of file swoc_meta.h.