launch a thread running an inherited templated method

47 Views Asked by At

I'm trying to have a class launch a inherited templated methode into multiple thread.

The basic idea is to have a class A be able to do work sequentially, and have a class B be able to run parallele execution of A (with custom arguments) to do the same work in parallel.

Both my classes and my methods have template ... which looks like it is an issue

Here is a basic exemple reproducing the behaviour. I know this exemple uses templates in a stupid way, but it's just to have a reproductible exemple.

#include <iostream>
#include <thread>
#include <vector>
#include <string>

template <typename T>
class A
{
  public: 
    template <typename U>
    void run(T n, U m) const
    { 
      std::cout << m << n << std::endl;
    }
};

template <typename T>
class B : public A<T>
{
  public:
    template <typename U>
    void run(T n, U m) const
    {
      std::cout << m << n << std::endl;
      std::vector<std::thread> threads;
      for (T i=0; i<n; ++i)
        threads.push_back(std::thread(&A<T>::run<U>, this, i, m));
      for (std::thread& thread : threads)
        thread.join();
    }
};

int main()
{
  A<int> a;
  a.run<const char*>(5, "simple try: ");
  B<size_t> b;
  b.run<std::string>(5, "inhereted case: ");
  return 0;
}

gcc (4.9.2) tell me that

src/threads.cc:27:34: erreur: expected primary-expression before ‘(’ token
 threads.push_back(std::thread(&A<T>::run<U>, this, i, m));
                              ^

Replacing std::thread(&A<T>::run<U>, this, i, m) with std::thread(&B<T>::run<U>, this, i, m) makes this code compile (and fork bomb as it's suposed to do)

0

There are 0 best solutions below