Template template parameters and "<<" operator (ostream)

For several days I have been trying to understand template template parameters. I tried to write an example using C++17 in Visual Studio, but I get this error:

Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'std::vector<int,std::allocator<int>>' (or there is no acceptable conversion)

For this example I tried to use a generic container from STL to highlight the idea of template template parameters in C++.

#include <iostream>
#include <vector>
using namespace std;

template<typename T, template<typename> typename Tpl_Type>
ostream& operator <<(ostream& out,const Tpl_Type<T>& x) {
    for (auto& aux : x)
        out << aux << "   ";
    out << endl;
    return out;
}

template<typename T, template<typename>typename Tpl_Type>
void functioon()
{
    
    Tpl_Type<T> vect2;
    for (auto i = 0; i < 5; i++) {
        vect2.push_back(i);
    }
    std::cout << vect2 << endl;
    return;
}

int main()
{

    functioon<int, std::vector>();
    return 0;
}

What am I doing wrong?

Upvotes: 0

Views: 337

Answers (1)

cigien
cigien

Reputation: 60440

std::vector is a template with more than 1 template parameter, so for functioon or operator<< to accept it as a template template parameter, that template template parameter itself needs to accept a variadic number of template parameters:

template<typename T, template<typename ...> typename Tpl_Type>
                                    // ^^^

Without making it variadic, clang and msvc will not compile the code. gcc compiles both versions, but I suspect this is a bug.

Here's a demo.

Upvotes: 2

Related Questions