0
#include <iostream>
#include <functional>
template<typename T,typename R>
struct match_struct{
  T t;
  std::function<R(T)> f;
};

template<typename T,typename R>
match_struct<T,R> match(T t,std::function<R(T)>f) {
  return match_struct<T,R>{t,f};
}

int main(int argc, char *argv[])
{
  auto m = match<int,int>(10, [](int i){ return i;});
  return 0;
}

I know that I have to specify the types explicitly when I want to create a struct/class. I thought that I could just create a templated function that would infer the types automatically.

So that I can just write

auto m = match(10, [](int i){ return i;});

Unfortunately that doesn't work.

Is there a workaround for this? So that I don't have to specify the types explicitly?

Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 2
    Basically, instead of having `match_struct` accept a `std::function`, you have it accept any arbitrary type like `template::type> match_struct match(T t, F f)`. – In silico Sep 06 '14 at 23:38
  • Here's a live example of what I'm talking about: http://coliru.stacked-crooked.com/a/30d09de001a7275a – In silico Sep 06 '14 at 23:44

0 Answers0