c++ - Traversing boost::variant types with Visitor that takes template -
i've got persistence class like:
class writer: private boost::noncopyable { template<typename t> struct record { std::vector<t> _queued; // waiting persisted hsize_t _filerows; // on disk dataset _ds; ... }; template<typename t> void write(writer::record<t>& rcrd) { ... } ...
that used persist types such as:
struct { sockaddr_in udpaddr; ... } struct b { uint8_t id; ... } struct c { ... } ...
i can't change apis above , want perform bulk operations on these heterogeneous types. i'm using boost::variant 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(); } }; // seems work -template argument substituted record<a>, record<b>,... struct writerecordvisitor : public boost::static_visitor<> { template <typename t> void operator()(t& operand) const { writer::write(operand); } }; // never compiles
then bulk operations across (many) heterogeneous types meant 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
no matching function call ...template substitution failed. cannot convert 'operand' (type writer::record<a>) type 'writer::record<record<a>>&'
which evidently looks wrong can't figure out what's causing it.
i'd either have writerecordvisitor approach working or able iterate through vector (obtaining boost::variant<...>) , somehow (template again, likely) use boost::get pass each appropriate element (record<a>, record<b>, ...) writer::write(record<t>).
i'd avoid defining visitor operator each possible heterogeneous type, defeat original goal of simplifying using heterogeneous container in 1st place.
i'm using gcc 4.7.2 on linux 3.5.4 fedora 17. appreciated -i did read other posts on boost::variant before posting.
Comments
Post a Comment