エンジニアのソフトウェア的愛情

または私は如何にして心配するのを止めてプログラムを・愛する・ようになったか

if N is included in X ...

// 演算子を定義する部分

template<typename T, typename U>
struct Operator
{
    explicit Operator(const T& lhs) : lhs(lhs) {}
    const T& lhs;
};

template<typename T, typename U>
inline Operator<T, U> operator % (const T& lhs, U)
{
    return Operator<T, U>(lhs);
}

enum ISINCLUDEDIN { isIncludedIn };

template<typename T, int N>
inline bool operator % (const Operator<T, ISINCLUDEDIN>& op, const T (&rhs)[N])
{
    for(int i = 0; i < N; ++i)
    {
        if(op.lhs == rhs[i])
        {
            return true;
        }
    }
    return false;
}
// 演算子を利用する部分

#include <iostream>
#include <string>

void foo(int n)
{
    static const int oddNumbers[] = { 1, 3, 5, 7, 9 };

    if(n %isIncludedIn% oddNumbers)
    {
        std::cout << n << " は1桁の奇数だ\n";
    }
    else
    {
        std::cout << n << " は1桁の奇数じゃないよ\n";
    }
}

void bar(const std::string& n)
{
    static const std::string oddNumbers[] = { "one", "three", "five", "seven", "nine" };

    if(n %isIncludedIn% oddNumbers)
    {
        std::cout << n << " は1桁の奇数だ\n";
    }
    else
    {
        std::cout << n << " は1桁の奇数じゃないよ\n";
    }
}

参照:http://d.hatena.ne.jp/E_Mattsan/20090305