Max*_*axB 0 c++ boost boost-variant
我已经读过boost::variant如果它的所有变体都是可流动的,那么它是可流动的.然而,
#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
}
Run Code Online (Sandbox Code Playgroud)
无法编译.为什么?
版本:
我没有检查序列化的文档,但我很确定需要通过Argument Dependent Lookup找到需要operator<<的类型,boost::variant否则就会出现在boost命名空间中.
这有效:
#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';
}
Run Code Online (Sandbox Code Playgroud)
输出:
3
1 1 1
3
Run Code Online (Sandbox Code Playgroud)