guard - TumpCpp リファレンス
namespace tump {
template <class Cond, class T>
struct if_clause {};
template <class T>
using otherwise = if_clause<std::true_type, T>;
namespace fn {
// 大元のメタ関数定義
template <class... Clauses>
struct guard {};
}
// 第一級関数化
template <std::size_t ArgsSize = 2>
using guard = cbk<fn::guard, ArgsSize>;
// メンバ型 type 呼び出し省略のエイリアステンプレート
template <class... Clauses>
using guard_t = typename fn::guard<Clauses...>::type;
}
クラス・パラメータ
- if_clause -
guardに指定する if 節。if-else if-elseのifやelseに該当 - otherwise -
guardに指定する otherwise 節。if-elseif-elseのelseや、switchのdefaultに該当 - Cond -
std::bool_constantを継承した型 - T - 任意の型
- ArgsSize -
guardに指定するif_clauseやotherwiseの数を指定 - Clauses - テンプレートパラメータパック。
if_clauseとotherwiseのみ指定可能
概要
tump::guardは、if-elseif-elseのような任意数の条件分岐を行うメタ関数です。
tump::guardが受け取る、型テンプレートパラメータパックClausesに指定可能な型は、tump::if_clauseとtump::otherwiseのみです。
※tump::otherwiseも実装上はtump::if_clauseの型エイリアスであり、厳密にはtump::if_clauseとその別名のみClausesに指定可能。
tump::guardを実行する際、Clausesは先頭から順番に評価されます。
Clausesの評価中最初に見つかった、パラメータCondが真と評価されたtump::if_clauseまたはtump::otherwiseの、パラメータTがtump::guardの結果として返却されます。
Clausesの一番最後にtump::otherwiseを指定することで、全てのtump::if_clauseに該当しない場合の結果を指定することができます。
テンプレートパラメータCondおよびTには、tump::expによる式も指定可能です。
tump::expを指定した際は、tump::guardを実行する際に、先にtump::expが評価され、その結果を判定したり、返却します。
なお、tump::if_clauseおよびtump::otherwiseはメタ関数ではありません。
あくまでも、tump::guardのパラメータClauses用に定義されたテンプレートクラスとなりますので、ご注意ください。
例
#include <tump.hpp>
// 符号なし整数、整数、浮動小数点、それ以外と型を判定し、
// それぞれの条件に合致した場合の型を返却。
template <class T>
using sample_guard1_t = tump::guard_t<
tump::if_clause<std::is_unsigned<T>, unsigned int>,
tump::if_clause<std::is_integral<T>, int>,
tump::if_clause<std::is_floating_point<T>, float>,
tump::otherwise<void>
>;
static_assert(std::is_same_v<sample_guard1_t<std::size_t>, unsigned int> == true);
static_assert(std::is_same_v<sample_guard1_t<char>, int> == true);
static_assert(std::is_same_v<sample_guard1_t<double>, float> == true);
static_assert(std::is_same_v<sample_guard1_t<std::tuple<int>>, void> == true);
// ラムダ式における guard の条件分岐の使用例。
// if_clause や otherwise のパラメータに指定された exp は先に評価される。
using sample_guard2 = tump::lambda<
tump::lambda_args<tump::_arg1, tump::_arg2>,
tump::exp<
tump::guard<3>,
// 2 つの引数が等しい場合、std::true_type を返却
tump::if_clause<
std::is_same<tump::_arg1, tump::_arg2>,
tump::exp<tump::_arg1, tump::_eq, tump::_arg2>
>,
// 2 つの引数が異なり、第一引数が整数の場合 int を返却
tump::if_clause<
tump::exp<
tump::is_not_same, tump::_arg1, tump::_arg2,
tump::_and,
tump::is_integral, tump::_arg1
>,
int
>,
// 上記 2 つの条件に一致しない場合、void を返却
tump::otherwise<void>
>
>;
static_assert(tump::eval<sample_guard2, int, int>::value == true);
static_assert(tump::eval<sample_guard2, long, double, tump::_eq, int>::value == true);
static_assert(tump::eval<sample_guard2, tump::list<>, double, tump::_eq, void>::value == true);
int main() {}