Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Created April 3, 2014 01:20
Show Gist options
  • Save Fiona-J-W/9946649 to your computer and use it in GitHub Desktop.
Save Fiona-J-W/9946649 to your computer and use it in GitHub Desktop.
A short example for a simple find_if using tmp
#include <iostream>
#include <type_traits>
template<bool B, unsigned Index, template<typename> class Predicate, typename...Tail>
struct find_if_helper2;
template<template<typename> class Predicate, unsigned Index, typename Head, typename...Tail>
struct find_if_helper1{
using type = typename find_if_helper2<Predicate<Head>::value, Index, Predicate, Tail...>::type;
};
template<bool B, unsigned Index, template<typename> class Predicate, typename...Tail>
struct find_if_helper2{
using type = typename find_if_helper1<Predicate, Index + 1, Tail...>::type;
};
template<unsigned Index, template<typename> class Predicate, typename...Tail>
struct find_if_helper2<true, Index, Predicate, Tail...>{
using type = std::integral_constant<unsigned, Index>;
};
template<unsigned Index, template<typename> class Predicate>
struct find_if_helper2<false, Index, Predicate>{
using type = void;
};
template<template<typename> class Predicate, typename...List>
struct find_if_impl {
using type = typename find_if_helper1<Predicate, 0, List...>::type;
};
template<template<typename> class Predicate, typename...List>
using find_if = typename find_if_impl<Predicate, List...>::type;
int main(){
// std::cout << find_if<std::is_integral, float, double, float>{} << '\n'; // Error
std::cout << find_if<std::is_integral, float, double, char, float>{} << '\n'; // 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment