vendredi 25 août 2017

Template template parameter simple example

First I was learning about template template parameters, and I started wondering if I had a vector<vector<int>>, if I could make a template that extracts out the type int from there.

But, in the process of trying to build an example, I can't even get a single-level template parameter template function to work!

#include <iostream>
#include <vector>

template< 
    template<class> class C2,
    class I
>
void for_2d(const C2<I>& container) 
{
    for( auto j : container ){
        std::cout << j;
    }
}

int main() {
    std::vector<int> cont;
    for_2d(cont);
    return 0;
}

This produces:

17 : <source>:17:5: error: no matching function for call to 'for_2d'
    for_2d(cont);
    ^~~~~~
8 : <source>:8:6: note: candidate template ignored: substitution failure : template template argument has different template parameters than its corresponding template template parameter
void for_2d(const C2<I>& container) 
     ^
1 error generated.
Compiler exited with result code 1

Aucun commentaire:

Enregistrer un commentaire