首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

C++11编程实践:兑现编译期的“或”运算

2013-01-09 
C++11编程实践:实现编译期的“或”运算#include type_traitsusing namespace stdtemplatebool C, typena

C++11编程实践:实现编译期的“或”运算

#include <type_traits>using namespace std;template<bool C, typename... Rest>struct or_impl : true_type{};template<typename Second, typename... Rest>struct or_impl<false, Second, Rest...> : or_impl<Second::value, Rest...>{};template<>struct or_impl<false> : false_type{};template<typename First, typename Second, typename... Rest>struct or_ : or_impl<First::value, Second, Rest...>{};int main(){struct unknown;static_assert(or_<true_type, true_type>::value, "error");static_assert(or_<false_type, true_type>::value, "error");static_assert(or_<true_type, false_type>::value, "error");static_assert(!or_<false_type, false_type>::value, "error");static_assert(or_<true_type, unknown>::value, "error");static_assert(or_<true_type, unknown, unknown>::value, "error");}

热点排行