Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ auto detection of template arguments?

Tags:

c++

templates

I am trying to define a recursive construct like a task farming. Here, I am trying for two operands which recursively can work for any number of operands as it can nest itself.

template <typename T1, typename T2>
class Farm
{
  private:
    T1 *task1;
    T2 *task2;
  public:
    // save them so that I can use them when invoking call operator
    Farm(T1 *_t1, T2 *_t2): task1(_t1), task2(_t2) { }

    void operator()()
    {
      // invoke call operator, meaning a farm could be a task (arbitrary nesting)
      (*task1)();
      (*task2)();
    }
};
int main()
{
    ... create two pointer(A *a, B *b...)
    Farm(a,b); // error: missing template arguments before ‘(’ token

    Farm<A, B>(a,b); // in this works, it works
}

The problem is with auto-detection of template arguments which is not working in this case. What am I doing wrong and how could I achieve this template parameters implicit detection by gcc compiler.

Thanks!

like image 526
usman Avatar asked Jun 15 '11 14:06

usman


1 Answers

Before C++17, classes/constructors didn't autodetect types like functions do. You need to write a wrapper function to create your class.

This is done as follows and called the Object Generator pattern. (thanks @Itjax!)

template <typename T1, typename T2>
Farm<T1, T2> makeFarm(T1* a, T2* b) {
      return Farm<T1,T2>(a,b);
}

// silly example
Farm<T1,T2> farm = makeFarm(a,b);

// better example
template<typename T>
void plow(T& farm) { farm.applyTractor(...); }

void blah() {
    plow(makeFarm(b,a)) 
}

This pattern emerges quite a lot when using lambda/bind/foreach and similar parts, when you want to create a temporary object of a templated class with some arguments and avoid specifying their type, usually sending it into another template function (std::for_each) or polymorphic object (std::function).

Note: The generator function usually inlines and with copy-elision optimization there will probably be no copy-constructor(s) called in your code at all. If you cannot copy your object, makeFarm() should return a smart pointer instead (std::unique_ptr is preferred in modern C++).

like image 163
Macke Avatar answered Oct 12 '22 09:10

Macke