0

In the example below I print a vector of integers (operator<< inside main). I have provided two implementations of the operator. I would like to understand why the first one matches and the second does not. How could I make the second one match?

#include <iostream>
#include <vector>

template< typename value_type, typename allocator_type >
std::ostream& operator<< ( 
    std::ostream& os, 
    std::vector< value_type, allocator_type >const& v
)
{
    os << "[vector-1]" << std::endl;
    return os;
}

template< typename VECTOR >
std::ostream& operator<< ( 
    std::ostream& os, 
    std::vector< typename VECTOR::value_type, typename VECTOR::allocator_type >const& v
)
{
    os << "[vector-2]" << std::endl;
    return os;
}

int main() {
    std::vector v = {1, 2, 3, 4};
    std::cerr << v;
}
az5112
  • 590
  • 2
  • 11
  • There isn't a unique answer for what `VECTOR` should be, so it isn't deduced from the call site. e.g. substituting `std::set` for `VECTOR` would match too – Caleth Oct 28 '22 at 13:29
  • Is there a unique answer for what `value_type` should be? And then, the second argument for `operator <<` is `std::vector` in either case - so... ? – az5112 Oct 28 '22 at 14:00
  • The issue is that it doesn't know *what* to get `::value_type` from – Caleth Oct 28 '22 at 14:05

0 Answers0