So I wrote an object that could handle JSON-like content, of which there is a minimal example:
#include <iostream>
#include <vector>
#include <variant>
struct Array;
struct Value;
struct Array: public std::vector<Value>{
using std::vector<Value>::vector;
};
struct Value: public std::variant<bool, int, Array>{
using std::variant<bool, int, Array>::variant;
};
Now I wanted to overload the output stream operator. Here is the code:
std::ostream& operator <<(std::ostream& os, const Value value);
std::ostream& operator <<(std::ostream& os, const Array array);
std::ostream& operator <<(std::ostream& os, const Array array){
for (auto &a : array){
os << a << ", ";
}
return os;
}
std::ostream& operator <<(std::ostream& os, const Value value){
os << value;
return os;
}
Now, if I try to run it :
int main()
{
Array a {1,2,3}; // OK
std::cout << a; // Segfault
}
Where's the catch ?
EDIT As pointed, there was a recursion loop.