When we have two operators for outputting an object and an array of these objects, and we try to output an array of constant objects, the operator for objects is involved. Is there any way to get the operator for arrays to work with arrays of persistent objects?
Code example:
#include <iostream>
#include <cstddef>
using std::size_t;
namespace nm
{
struct C { int i; };
template <size_t N>
using c_c_array = C[N];
inline std::ostream& operator << (std::ostream& lhs, C const*) { return lhs << "1\n"; }
template <size_t N>
inline std::ostream& operator << (std::ostream& lhs, c_c_array<N> const&) { return lhs << "2\n"; }
}
int main()
{
nm::C c{1};
nm::C arr[5];
nm::C const c_const{1};
nm::C const arr_const[5] {{1}, {1}, {1}, {1}, {1}};
std::cout << &c
<< arr
<< &c_const
<< arr_const;
return 0;
}
Exit: 1 2 1 1
In addition, operator 2 in my case uses output 1.
source
share