Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

c++ - variadic templates with template function names

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.

in sensor.hpp

struct EdgeSensor //a mixin
{
    void update(){}
    void printStats() {}
};

struct TrendSensor //another mixin
{
    void update(){}
    void printStats() {}
};

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
    void update() /*{ what goes in here??? }*/
    void printStats() /*{ what goes in here??? }*/
};

in sensor.t.hpp

template<typename ... SensorType>
void BaseSensor<SensorType...>::update()
{
    int arr[] = { (SensorType::update(), 0)..., 0 };
    (void)arr;
}

template<typename ... SensorType>
void BaseSensor<SensorType...>::printStats()
{
    int arr[] = { (SensorType::printStats(), 0)..., 0 };
    (void)arr;
}

in main.cpp

int main(int , const char **) 
{
    {
        BaseSensor<EdgeSensor,TrendSensor> ets;
        ets.update();
        ets.printStats();
    }
    {
        BaseSensor<EdgeSensor> ets;
        ets.update();
        ets.printStats();
    }
}

The above code executes the update() of all the mixins in turn, before going on to execute all the printStats() from all the mixins as well.

I wonder if it is somehow possible to avoid duplicating the implementation of BaseSensor::update() and BaseSensor::printStats() and create a generic (template) function that accepts the name of the target function to execute across all the mixins:

For example, I could create a method runAll()

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
    void update() /*{ what goes in here??? }*/
    void printStats() /*{ what goes in here??? }*/

    template<typename FnName>
    void runAll(FnName f)
    {
        int arr[] = { (SensorType::f(), 0)..., 0 };
        (void)arr;
    }
};

How would I call it then from BaseSensor::update() and BaseSensor::printStats(). I have attempted to use

void update() { runAll<update>(); }
void printStats() { runAll<printStats>(); }

but this does not work (did not expect it to). The problem with passing function name as a function argument (which I see is many other questions such as here is that I do not know how to point to various ::update() functions from BaseSensor::update(). for example

void update() { runAll<update>( update() ); }

is also not correct.

Is it possible to avoid copying in this case? How would the template parameters look like if I where to move a working runAll() into file "sensor.t.hpp" ?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a generic lambda and a kind of inversion of control.
It follows a minimal, working example:

#include<iostream>

struct EdgeSensor
{
    void update() { std::cout << "EdgeSensor::update" << std::endl; }
    void printStats() { std::cout << "EdgeSensor::printStats" << std::endl; }
};

struct TrendSensor
{
    void update() { std::cout << "TrendSensor::update" << std::endl; }
    void printStats() { std::cout << "TrendSensor::printStats" << std::endl; }
};

template<typename ... SensorType>
class BaseSensor : public SensorType ...
{
    template<typename F>
    void execute(F &&f) {
        int arr[] = { (f(static_cast<SensorType&>(*this)), 0)..., 0 };
        (void)arr;
    }

public:
    void update() {
        execute([](auto &t) { t.update(); });
    }

    void printStats() {
        execute([](auto &t) { t.printStats(); });
    }
};

int main() {
    BaseSensor<EdgeSensor,TrendSensor> ets;
    ets.update();
    ets.printStats();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...