I've got a persistence class like:
class Writer: private boost::noncopyable {
template<typename T>
struct Record {
std::vector<T> _queued; // waiting to be persisted
hsize_t _fileRows; // on disk
DataSet _ds;
...
};
template<typename T>
void write(Writer::Record<T>& rcrd) { ... }
...
That is used to persist Types such as:
struct A {
sockaddr_in udpAddr;
...
}
struct B {
uint8_t id;
...
}
struct C { ... }
...
I can't change the APIs above and I want to perform bulk operations on these heterogeneous types. I'm using boost::variant with partial success, following their own tutorial:
typedef boost::variant< Record<A>, Record<B>, Record<C>, ...> _types;
std::vector<_types> _records;
struct CloseRecordVisitor : public boost::static_visitor<> {
template <typename T>
void operator()(T& operand) const {
assert(operand._queued.empty());
operand._ds.close();
}
}; // This seems to work -template argument is substituted with Record<A>, Record<B>,...
struct WriteRecordVisitor : public boost::static_visitor<> {
template <typename T>
void operator()(T& operand) const {
Writer::write(operand);
}
}; // This never compiles
Then the bulk operations across all the (many) heterogeneous types was meant to do simply:
CloseRecordVisitor _closeRecordVisitor;
std::for_each(_records.begin(), _records.end(), boost::apply_visitor(_closeRecordVisitor));
WriteRecordVisitor _writeRecordVisitor;
std::for_each(_records.begin(), _records.end(), boost::apply_visitor(_writeRecordVisitor));
WriteRecordVisitor doesn't compile. Error is
No matching function to call ...template substitution failed. Cannot convert 'operand' (type Writer::Record<A>) to type 'Writer::Record<Record<A>>&'
Which evidently looks wrong but I can't figure out what's causing it.
I'd like to either have the WriteRecordVisitor approach working or be able to iterate through the vector (obtaining boost::variant<...>) and somehow (template again, likely) use boost::get to pass each appropriate element (Record<A>, Record<B>, ...) to Writer::write(Record<T>).
I'd also like to avoid defining a visitor operator for each possible heterogeneous type, as that would defeat the original goal of simplifying using an heterogeneous container in the 1st place.
I'm using gcc 4.7.2 on Linux 3.5.4 Fedora 17. Any help is appreciated -I did read all the other posts on boost::variant before posting.