KerrekSBの代替ソリューションは次のとおりです。
#include <tuple>
#include <type_traits>
#include <cstdlib>
template <typename HeadPred, typename ...TailPreds>
struct CombinePredAnd
{
template<typename H, typename ...Ts>
explicit CombinePredAnd(H const & h, Ts const &...ts)
: _preds(h, ts...){}
template <typename T>
bool operator()(T t){
return eval(t,_preds);
}
private:
template<typename T, size_t I = 0, typename ...Ps>
typename std::enable_if<sizeof ...(Ps) == I,bool>::type
static eval(T t, std::tuple<Ps...>) {
return true;
}
template<typename T, size_t I = 0, typename ...Ps>
typename std::enable_if<sizeof ...(Ps) != I,bool>::type
static eval(T t, std::tuple<Ps...> const & preds) {
auto const & pred = std::get<I>(preds);
return pred(t) && eval<T,I + 1>(t,preds);
}
std::tuple<HeadPred, TailPreds...> _preds;
};
これは、Kerrek SBが、接続詞または論理和の選択によってパラメーター化される任意の述語関数の一般的な接続詞または論理和に提案する方法で、次のように一般化できます。テストプログラムが追加され、gcc4.7.2とclang3.2でビルドされています。
#include <tuple>
#include <type_traits>
#include <functional>
#include <cstdlib>
template <class AndOrOr, typename HeadPred, typename ...TailPreds>
struct dis_or_con_join
{
static_assert(
std::is_same<AndOrOr,std::logical_and<bool>>::value ||
std::is_same<AndOrOr,std::logical_or<bool>>::value,
"AndOrOr must be std::logical_and<bool> or std::logical_or<bool>");
template<typename H, typename ...Ts>
explicit dis_or_con_join(H const & h, Ts const &...ts)
: _preds(h, ts...){}
template <typename T>
bool operator()(T t){
return eval(t,_preds);
}
private:
static const bool conjunction =
std::is_same<AndOrOr,std::logical_and<bool>>::value;
template<typename T, size_t I = 0, typename ...Ps>
typename std::enable_if<sizeof ...(Ps) == I,bool>::type
static eval(T t, std::tuple<Ps...>) {
return conjunction;
}
template<typename T, size_t I = 0, typename ...Ps>
typename std::enable_if<sizeof ...(Ps) != I,bool>::type
static eval(T t, std::tuple<Ps...> const & preds) {
auto lamb = conjunction ?
[](bool b){ return b; } :
[](bool b){ return !b; };
auto const & pred = std::get<I>(preds);
return lamb(lamb(pred(t)) && lamb(eval<T,I + 1>(t,preds)));
}
std::tuple<HeadPred, TailPreds...> _preds;
};
template<typename HeadPred, typename ...TailPreds>
using conjunction =
dis_or_con_join<std::logical_and<bool>,HeadPred,TailPreds...>;
template<typename HeadPred, typename ...TailPreds>
using disjunction =
dis_or_con_join<std::logical_or<bool>,HeadPred,TailPreds...>;
// Test...
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
// For various predicates with various constructors...
template<typename T>
struct is_in_list
// Stores an arbitrary sized list of its type T constructor arguments
// and then with tell us whether any given T is in its list
{
is_in_list(initializer_list<T> il)
: _vals(il.begin(),il.end()){}
bool operator()(T t) const {
return find(_vals.begin(),_vals.end(),t) != _vals.end();
}
list<T> _vals;
};
int main()
{
is_in_list<char> inl03 = {'\0','\3'};
is_in_list<long> inl013 = {0,1,3};
is_in_list<float> inl0123 = {0.0f,1.0f,2.0f,3.0f};
conjunction<is_in_list<char>,is_in_list<long>,is_in_list<float>>
conj{inl03,inl013,inl0123};
disjunction<is_in_list<char>,is_in_list<long>,is_in_list<float>>
disj{inl03,inl013,inl0123};
cout << "conjunction..." << endl;
cout << 1 << " is " << (conj(1) ? "" : "not ")
<< "in all the lists" << endl;
cout << 0 << " is " << (conj(0) ? "" : "not ")
<< "in all the lists" << endl;
cout << 3 << " is " << (conj(3) ? "" : "not ")
<< "in all the lists" << endl;
cout << "disjunction..." << endl;
cout << 1 << " is in " << (disj(1) ? "at least one " : "none ")
<< "of the lists" << endl;
cout << 2 << " is in " << (disj(2) ? "at least one " : "none ")
<< "of the lists" << endl;
cout << 3 << " is in " << (disj(3) ? "at least one " : "none ")
<< "of the lists" << endl;
cout << 4 << " is in " << (disj(4) ? "at least one " : "none ")
<< "of the lists" << endl;
return 0;
}
おそらく不明瞭な行:
return lamb(lamb(pred(t)) && lamb(eval<T,I + 1>(t,preds)));
同等性を利用するだけです。
P or Q = not(not(P) and not(Q))
出力:-
conjunction...
1 is not in all the lists
0 is in all the lists
3 is in all the lists
disjunction...
1 is in at least one of the lists
2 is in at least one of the lists
3 is in at least one of the lists
4 is in none of the lists