Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::static_if< V > Struct Template Reference

A metaprogramming facility for selecting a branch statically without evaluating both branches apriori. More...

Detailed Description

template<bool V>
struct arene::base::static_if< V >

A metaprogramming facility for selecting a branch statically without evaluating both branches apriori.

This has uses similar to std::conditional except that it allows for lazily evaluated metafunctions. Usage can lead to fewer template instantiations, which can have significant impact on compile times in low level facilities.

Note
This is a rather advanced metaprogramming facility. If you aren't sure if you need it, you probably don't, and should prefer the more conventional std::conditional .

The principle that's followed is that instantiations of type templates are costly in terms of resource usage during compilation and have a tendency to add up surprisingly quickly, however, template aliases are close to free (you only pay for the substitution process of the template arguments). static_if minimizes resource usage at compile-time because regardless of how many times a developer uses static_if , with any combination of arguments, there are only two types: static_if<true> and static_if<false> . All branching is done with nested templates aliases.

In order to handle a variety of branch kinds, there are 4 different forms of "then else" corresponding to when none, one, or both branches take a lazily-evaluated metafunction. The naming convention used is that apply in the name corresponds to a parameter that is a metafunction. In other words:

then_else // No metafunctions then_apply_else // The "then" branch is a metafunction then_else_apply // The "else" branch is a metafunction then_apply_else_apply // Both "then" and "else" branches are metafunctions

Usage Example:

// Use a lock-free queue if T is trivially-copyable, otherwise a fallback.
template <class T>
using queue_type
= typename static_if<std::is_trivially_copyable_v<T>> // The condition
::template then_apply_else_apply<
lockfree_queue, // A lockfree queue template
queue_with_locks, // A queue-with-locks template
T // The parameters to pass to the chosen template
>;
A metaprogramming facility for selecting a branch statically without evaluating both branches apriori...
Definition static_if.hpp:48

The documentation for this struct was generated from the following file: