1 #include <iostream> 2 template <typename ... Ts> 3 struct CNAny { 4 static bool Do(int i) { 5 return (Ts::Do(i) || ...); 6 } 7 }; 8 9 template <typename ... Ts> 10 struct CNAll { 11 static bool Do(int i) { 12 return (Ts::Do(i) && ...); 13 } 14 }; 15 16 struct CNa { 17 static bool Do(int i) { 18 return i > 5; 19 } 20 }; 21 22 struct CNb { 23 static bool Do(int i) { 24 return i < 10; 25 } 26 }; 27 28 #define TEST(e) do {\ 29 bool yes = (e); \ 30 std::cout << #e << " = " << yes << std::endl; \ 31 } while (0) 32 33 int main() { 34 using CNAny_CNa_CNb = CNAny<CNa, CNb>; 35 TEST( CNAny_CNa_CNb::Do(1) ); 36 TEST( CNAny_CNa_CNb::Do(7) ); 37 TEST( CNAny_CNa_CNb::Do(12) ); 38 39 using CNAll_CNa_CNb = CNAll<CNa, CNb>; 40 TEST( CNAll_CNa_CNb::Do(1) ); 41 TEST( CNAll_CNa_CNb::Do(7) ); 42 TEST( CNAll_CNa_CNb::Do(12) ); 43 44 return 0; 45 }
输出内容:
CNAny_CNa_CNb::Do(1) = 1 CNAny_CNa_CNb::Do(7) = 1 CNAny_CNa_CNb::Do(12) = 1 CNAll_CNa_CNb::Do(1) = 0 CNAll_CNa_CNb::Do(7) = 1 CNAll_CNa_CNb::Do(12) = 0