getRatio
()機能をint, long, double and float
に限定する場合は、この機能も使用できます。 char
型の引数を指定して呼び出すと、 "意味のある"コンパイルエラーが生成されます。コンパイルエラーはthis_type_is_not_allowed_in_getRatioです。
//yourheader.h
template<typename T>
inline T getRatio(T numer, T denom)
{
typedef typelist<int, typelist<long, typelist<double, float>>> allowedtypes;
compile_time_checker<contains<allowedtypes, T>::result> this_type_is_not_allowed_in_getRatio;
return (numer/denom);
}
それは、このヘッダーを使用しています。
//metafunctions.h
template<typename H, typename T>
struct typelist
{
typedef H Head;
typedef T Tail;
};
template<typename T, typename Tail>
struct contains
{
static const bool result = false;
};
template<typename Head, typename Tail, typename T>
struct contains<typelist<Head, Tail>, T>
{
static const bool result = false || contains<Tail, T>::result;
};
template<typename T, typename Tail>
struct contains<typelist<T, Tail>, T>
{
static const bool result = true || contains<Tail, T>::result;
};
template<bool b> struct compile_time_checker;
template<> struct compile_time_checker<true> {};
希望、それはあなたを助けます。 すべてのコードをただ1つの関数で書くことができます!
だから、それは長い、ダブル、フロート、および他のタイプでのみ動作するようにしたいですか? – EboMike
@EboMike。はい.. – blueskin