خرید بک لینک

Vote count: 1

Is there a way to extract a typedef from a template class? For example, this is what I would like to do:

template<typename T, typename... Args>
class Foo{
public:
   typedef T(*Functor)(Args...);
   Foo() = default;
};

template<typename T, typename... Args>
Foo<T, Args...> make_foo(T(*f)(Args...)){
    retu Foo<T, Args...>;
}

int bar(int i){
    retu i * 2;
}

using type = make_foo(bar)::Functor;

I caot do this. However, I can do this:

using type = Foo<int, int>::Functor;

This sort of defeats the purpose for me. Is there any way to wrap a function such that I can extract it in type form?

asked 5 mins ago

2 Answers

Vote count: 3

Would decltype be good enough?

using type = decltype(make_foo(bar))::Functor;

answered 3 mins ago

Vote count: 3

Use decltype:

template<typename T, typename... Args>
class Foo{
public:
   typedef T(*Functor)(Args...);
   Foo() = default;
};

template<typename T, typename... Args>
Foo<T, Args...> make_foo(T(*f)(Args...)){
    retu Foo<T, Args...>{}; // Small compilation error fixed here.
}

int bar(int i){
    retu i * 2;
}

using type = decltype(make_foo(bar))::Functor;

This operator retus the type of the expression it is fed with.

answered 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 7 ارديبهشت 1395 ساعت: 2:03

صفحه بندی