Skip to content

Instantly share code, notes, and snippets.

@5a494d
Forked from emaxerrno/template_lambda.cc
Created September 18, 2018 14:28
Show Gist options
  • Save 5a494d/3bcae9af6d3a7d24ff652e5c88c15cb0 to your computer and use it in GitHub Desktop.
Save 5a494d/3bcae9af6d3a7d24ff652e5c88c15cb0 to your computer and use it in GitHub Desktop.
template_lazy_closures.cc
#include <type_traits>
#include <iostream>
// This template is called 'or_combinator'
// it takes 2 function templates 'func1' , and 'func2'
// the arguments to each type function is template<typename>
// which means any typename
// you invoke them via func1<T>::value
// but they aren't instantiated until you ask for or_combinator<>::lambda<>::value
//
// they are also lazy! by nature of compiler laziness
//
template <template<typename> class func1, template<typename> class func2>
struct or_combinator{
template<typename T> struct lambda{
// notice that lambda
// is a true closure over the type functions func1 and func2
//
// Obviously they have to have a member type called value
// That's what's called the return type of a template
//
constexpr static const bool value = func1<T>::value || func2<T>::value;
};
};
// templates to demonstrate late binding of function templates
template <typename T> struct is_ptr {
constexpr const static bool value = false;
};
template <typename T> struct is_ptr<T*>{
constexpr const static bool value = true;
};
template <typename T> struct is_const {
constexpr const static bool value = false;
};
template <typename T> struct is_const<const T> {
constexpr const static bool value = true;
};
int main(){
std::cout << "This late binding should blow your mind.\n Value: "
<< or_combinator<is_ptr,is_const>::lambda<const int>::value
<< std::endl
<< "The reason is true, is because is_const<const int>::value is true"
<< std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment