I'm stuck with this why trying to overlap ostream of std::array in c . (error: ambiguous overload for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream'} and 'const char [2]') here is my script:
#include <bits/stdc .h>
using namespace std;
template<typename T>
ostream& operator <<(ostream& out, T& arr)
{
bool pre = false;
for(auto& i : arr)
{
if(pre) out << ' ';
pre = true;
out << i;
}
return out;
}
signed main()
{
array<int, 4> a = {123, 123, 12, 12};
cout << a << "\n";
return 0;
}
CodePudding user response:
This forces the overload to take only std::array, which should solve your ambiguous overload problem:
template<typename T, std::size_t N>
ostream& operator <<(ostream& out, const std::array<T, N> &arr)
{
bool pre = false;
for(auto& i : arr)
{
if(pre) out << ' ';
pre = true;
out << i;
}
return out;
}
