c++ - Defining a variadic template type with unknown argument list as std::map value type -
i not sure if possible @ trying achieve past 3 hours without sense-making approach.
i have variadic template this:
template<typename... ts> class xunit_test_item { public: template<typename f, typename... args> xunit_test_item(const std::string& name, f&& func, args&&... args) : m_name(name) , m_f(std::forward<f>(func)) , m_args(std::forward<args>(args)...) { } // ... private: const std::string m_name; std::function<void (ts...) > m_f; std::tuple<ts...> m_args; };
above template wraps test item holder of functor passed arguments along string name item.
objects of type have stored std::map value type. handled in class contains std::map
container member, registration method add xunit_test_item items , run method, iterates on map , executes stored xunit_test_item stored functions.
the class looks this:
class unit_test { // define map type of xunit_test_items template<typename ...ts> using testitem_map_t = std::map<size_t, xunit_test_item<ts...>>; public: unit_test() = delete; unit_test(const std::string& group_name) : m_tm() , m_group_name(group_name) , m_test_id(0) { } // register functor item template<typename f, typename ...args> void add_test(const std::string& name, f&& fun, args&& ...args) { m_tm.emplace(++m_test_id, xunit_test_item<args...>( name, std::forward<f>(fun), std::forward<args>(args)...)); } void run() { // ... (auto& t : m_tm) { // ... t.second->run(); // ... } } private: testitem_map_t m_tm; // <-- compiler correctly reports error std::string m_group_name; size_t m_test_id; };
the problem have find right syntax / different approach define xunit_test_item
value type of m_tm
map member. above code not compiling - without doubt - have specify argument list m_tm
member type testitem_map_t. have no idea how define type , declare correctly m_tm
member. argument list unknown @ point. perhaps above classes have design issue, wonder if there more or less simple way achieve this. may fail see simple solution.
would happy learn , suggestion. in advance!
update
fresh day, fresh try , cup of coffee ...
i refactored body of add_test
method using 2-level lambda , seems work far. test functions (the actual test cases) can add lambdas, function pointers arbitrary or no arguments @ all.
i don't know if best solution, or if can coded in better way, maybe wants comment on topic. reason why not enter answer. hope initial question maybe learn c++14 sugar solution ... if possible.
this first tested version ...
template<typename f, typename ...args> void add_test(const std::string& name, f&& fun, args&& ...args) { std::function<void(args...)> fun_xargs = [name, fun](auto&&... args) { std::cout << " inner lambda\n"; auto x = xunit_test_item<args...>(name, fun, std::forward<args>(args)...); x.run(); }; std::function<void()> fun_void = [=] { std::cout << "outer lambda\n"; fun_xargs(args...); }; m_tm.emplace(++m_test_id, fun_void); }
it seems need:
class unit_test { public: unit_test() = delete; unit_test(const std::string& group_name) : m_tm(), m_group_name(group_name) { } // register functor item void add_test(const std::string& name, std::function<void()> f) { m_tm.emplace(name, std::move(f)); } void run() { // ... (const auto& t : m_tm) { // ... t.second->run(); // ... } } private: std::map<std::string, std::function<void()>> m_tm; std::string m_group_name; };
Comments
Post a Comment