Search code examples
c++c++11variadic-templatesreference-wrapper

Passing references to a variadic-templates use the std::reference-wrapper


I try to pass to a variadic template function a list of references and pass it to another function. The code that I wrote is the following:

template <typename T>
void fun(cv::Point_<T> & pt) { pt.x++; pt.y++; }

template <class ... args>
void caller(args & ... list) {

    typedef typename std::tuple_element<0, std::tuple<args...> >::type T;

    std::array<std::reference_wrapper<T>, sizeof...(list)> values {list ...     };

    for(int i=0; i<values.size(); i++)
      fun(values[i]);

}

then I call the function caller in this way:

cv::Point2f a, b, c;

caller(a, b, c);

the compiler give me the following error:

No matching function for call to 'fun'
Candidate template ignored: could not match 'Point_' against 'reference_wrapper'

what I missing?


Solution

  • Although std::reference_wrapper<T> has an implicit conversion to T&, you cannot use both an implicit conversion and template argument deduction at the same time, and template argument deduction is necessary to call fun.

    Try

    fun(values[i].get());