Search code examples
c++multithreadingtemplatestype-deductiontemplate-argument-deduction

Get types of classes for templates


I have template function as this:

template<class RandomAccessIterator, class T, class Func>
T reduce(RandomAccessIterator first, RandomAccessIterator last, const T& initial_value, Func func) {
    // Some code;
    return cur_value;
}

I can't start thread by:

std::thread t1(reduce, iterator1, iterator2, 0, sum)

Because reduce function isn't created due to template behavior. I must write manually all types of defined in template. Like:

std::thread t1(reduce<SomeIterator, SomeNumber, SomeFunc>, iterator1, iterator2, 0, sum)

If I have another types I should write:

std::thread t1(reduce<AnotherIterator, AnotherNumber, Func>, iterator1, iterator2, 0, sum)

How can I automate this process? Is there a method to calculate types of classes like:

std::thread t1(reduce<typeof(X), typeof(Y), typeof(Z)>, iterator1, iterator2, 0, sum)

Solution

  • Just use a lambda.

    std::thread t1([=](){reduce(iterator1, iterator2, 0, sum));
    

    Magic!