peterSweter
peterSweter

Reputation: 653

Template as a parameter of function template - deduction fail

I want to create template like this which can deduce TT class template an T type:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

template < template <typename> class TT, typename T>
T f(TT<T*> & A ){
    cout << "it works\n";
    return *A[0];
};


int main(){
    vector<int*> v;
    f(v);
    return 0;
}

I have an error (with clang-4.0):

temded2.cpp: In function ‘int main()’:
temded2.cpp:20:21: error: no matching function for call to ‘f(std::vector<int*>&)’
     f<std::vector>(v);
                     ^
temded2.cpp:12:3: note: candidate: template<template<class> class TT, class T> T f(TT<T*>&)
 T f(TT<T*> & A ){
   ^

I think that TT should be equal to std::vector and T should be equal to int, what am i doing wrong ?

Upvotes: 2

Views: 85

Answers (3)

Exagon
Exagon

Reputation: 5088

Since vector is a template with two template parameter, one for the type of the elements, and one for the allocator,

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

Your temlate parameter B also needs two template parameters, so the function looks like this:

template <template <typename, typename> class V, typename T, typename A>
T f(V<T, A>& arg ){
    cout << "it works\n";
    return arg[0];
};

Now the function works. live demo

Upvotes: 2

WhozCraig
WhozCraig

Reputation: 66194

Your template template-parameter isn't what you think it is. There's more to a std::vector<T> than you think, including default template parameters that you're not accounting for. Luckily, variadic arguments in C++11 will help you solve that problem

#include <iostream>
#include <vector>

template < template <typename, typename...> class TT, typename T, typename... Args>
T f(TT<T*, Args...> & A )
{
    std::cout << __PRETTY_FUNCTION__ << '\n';
    if (A.size() > 0)
        return *(A[0]);
    return T();
};

int main()
{
    std::vector<int*> v;
    f(v);
    return 0;
}

Output

T f(TT<T *, Args...> &) [TT = vector, T = int, Args = <std::__1::allocator<int *>>]

Note the Args above. Because those are missing in your very specific template template-parameter expected arg list, there is no match. As you can see, variadic arguments can solve that problem.

Upvotes: 4

Barry
Barry

Reputation: 302718

std::vector is a class template that takes two template parameters:

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

Your f expects a class template with only one template parameter. So it simply doesn't match.

Upvotes: 4

Related Questions