c++ - Could this templated syntax be improved? -
i have template method:
template <class somelhs, class somerhs, resulttype (somelhs::*callback)(somerhs&)> void add() { struct local { static resulttype trampoline(baselhs& lhs, baserhs& rhs) { return (static_cast<somelhs&>(lhs).*callback)(static_cast<somerhs&>(rhs)); } }; _back_end.template add<somelhs,somerhs>(&local::trampoline); }
currently i'm calling this:
tracker.add<quad, multi, &quad::track>(); tracker.add<quad, singl, &quad::track>(); tracker.add<sext, multi, &sext::track>(); ...
it working fine, don't have repeat 2 times name of class somelhs
. there way avoid that?
for people may have recognized it: yes, related basicfastdispatcher of alexandrescu, in particular i'm writing front end operate member functions.
i don't think can't improved particularly, unfortunate i'd love find way this.
template type deduction possible function template arguments , need pass in non-type member function pointer @ compile time in order treated name rather varying quantity. means having specify args.
i.e. can this:
template <class somelhs, class somerhs> void add(resulttype (somelhs::*callback)(somerhs&)) { ... } // nice syntax: tracker.add(&sext::track); // ugly overloaded functions, cast needed. // p.s. not sure right syntax without compiling it. tracker.add((resulttype (quad::*)(multi&) &quad::track);
but have actual pointer cannot subsequently used template parameter.
the thing think use macro, though arguable if improves syntax here. i'd adds unnecessary level of obfuscation.
e.g.
#define tmfn_args(c, m, p1) c, p1, &c::m tracker.add<tmfn_args(quad, track, multi)>();
edit:
however, if name of function 'track', along following lines:
template <typename c, typename p1> void addtrack() { add<c, p1, &c::track>(); } tracker.addtrack<quad, multi>();
Comments
Post a Comment