Reputation: 12567
I've read that a boost::variant
is streamable if all of its variants are streamable. However,
#include <iostream>
#include <vector>
#include <string>
#include <boost/variant.hpp>
std::ostream& operator<<(std::ostream& out, const std::vector<int>& v) {
for(int i = 0; i < v.size(); ++i)
out << " " << v[i];
return out;
}
int main() {
boost::variant<int, std::string > a(3);
std::cout << a << '\n'; // OK
std::vector<int> b(3, 1);
std::cout << b << '\n'; // OK
boost::variant<int, std::vector<int> > c(3);
std::cout << c << '\n'; // ERROR
}
fails to compile. Why?
Versions:
Upvotes: 0
Views: 204
Reputation: 55395
I haven't checked the documentation of serialization, but I'm pretty sure that operator<<
for the types of boost::variant
needs to be either found by Argument Dependent Lookup or else be present in boost
namespace.
#include <iostream>
#include <vector>
#include <string>
#include <boost/serialization/variant.hpp>
namespace boost {
std::ostream& operator<<(std::ostream& out, const std::vector<int>& v) {
for(int i = 0; i < v.size(); ++i)
out << " " << v[i];
return out;
}
}
int main() {
boost::variant<int, std::string > a(3);
std::cout << a << '\n';
{
using namespace boost;
std::vector<int> b(3, 1);
std::cout << b << '\n';
}
boost::variant<int, std::vector<int> > c(3);
std::cout << c << '\n';
}
Output:
3
1 1 1
3
Upvotes: 6