Skip to content

Instantly share code, notes, and snippets.

@zakky-dev
Created October 17, 2014 08:27
Show Gist options
  • Save zakky-dev/cef3901f48924272fb2e to your computer and use it in GitHub Desktop.
Save zakky-dev/cef3901f48924272fb2e to your computer and use it in GitHub Desktop.
templateにlambdaを渡したい
#include<iostream>
/*
// lambdaをtemplate parameterとして渡したい
auto template_func = [](int l, int r) { return l == r; };
typedef decltype(template_func) template_function_parameter;
template <template_function_parameter f>
bool template_function_bad()
{
return f(1,1);
}
*/
template <bool(*f)(int,int)>
bool template_function()
{
return f(1,23);
}
bool param_func(int r, int l)
{
return r == l;
}
int main()
{
// std::cout << template_function_bad<[](int l, int r){ return l == r; }>();
std::cout << template_function<*param_func>();
return 0;
}
@otf
Copy link

otf commented Oct 17, 2014

#include<iostream>

/*
// lambdaをtemplate parameterとして渡したい
auto template_func = [](int l, int r) { return l == r; };
typedef decltype(template_func) template_function_parameter;

template <template_function_parameter f>
bool template_function_bad()
{
  return f(1,1);
}
*/

template <class F>
bool template_function()
{
    F f;
  return f(1,23);
}

int main()
{
  //  std::cout << template_function_bad<[](int l, int r){ return l == r; }>();
    struct { bool operator()(int r, int l){ return r == l; } } f;
    std::cout << template_function<decltype(f)>();

  return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment