variadic templates with template function names

前端 未结 5 1148
悲哀的现实
悲哀的现实 2021-01-14 18:14

following this question , I am trying to avoid copy-pasting some code related to calling all of the same-named methods of the mixins of the class BaseSensor.

5条回答
  •  清歌不尽
    2021-01-14 18:58

    Another c++11 way could be to use std::array of pointer to method e.g.:

    template
    class BaseSensor : public SensorType ... //to my BaseSensor class
    {
       void runAll(std::array&& vs) {
          for (auto v: vs) {
             (this->*v)();
          }
       }
    
    public:
        void update() {
           runAll({&SensorType::update...});
        }
        void printStats() {
           runAll({&SensorType::printStats...});
        }
    };
    

提交回复
热议问题