Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing a set of pointers to member functions of a template class

I have here a minimized version of code below, that won’t compile.

#include<iostream>
#include<vector>
#include<set>
template<int D>
class Points{
public:
    std::vector<int> data;
    void func1()
    {
        // some operation on data which depends on D
        std::cout<<"Operation 1 which is " <<"D \n";
    }
    void func2()
    {
        // some operation on data which depends on D
        std::cout<<"Operation 2 which is " <<"D \n";
    }
    void execute_all(std::vector<void (Points::*)()> flist)
    {
        for (auto f: flist){
            (this->* f)();
        }
    }
};
template<int D>
using fptr = void (Points::*)();

int main()
{
    constexpr D = 2;
    Points p;
    std::set<fptr<D>> functionlist;
    functionlist.insert(&(Points<D>::func1));
    functionlist.insert(&(Points<D>::func2));
    p.execute_all(functionlist);
    return 0;
}

My intention is to group together function pointers, that I can pass to the member function in order to execute them together. In my intended code (not shown here) the "execute_all" happens in a deeply nested loop, with local arguments. I tried to run the above, even without a template class, but even that won’t compile: invalid use of non-static member function ‘void Points::func1()’
Please help by pointing me to the issues with my code, and possibly providing a corrected version, that executes all functions func1 and func2 as intented.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your fptr needs to use D to say to what Points member functions the pointer is actually a pointer type for.

template <int D>
using fptr = void (Points<D>::*)();

You are also mixing container types. You can’t assign a set to a vector.

Fixed:

#include <iostream>
#include <vector>

template <int D>
class Points {
public:
    std::vector<int> data;

    void func1() {
        // some operation on data which depends on D
        std::cout << "Operation 1 which is " << D << "D\n";
    }

    void func2() {
        // some operation on data which depends on D
        std::cout << "Operation 2 which is " << D << "D\n";
    }

    void execute_all(const std::vector<void (Points::*)()>& flist) {
        for (auto f : flist) {
            (this->*f)();
        }
    }
};

template <int D>
using fptr = void (Points<D>::*)();

int main() {
    constexpr int D = 2;                // int was missing here
    Points<D> p;                        // <D> was missing here
    std::vector<fptr<D>> functionlist;  // was a set
    functionlist.push_back(&Points<D>::func1);
    functionlist.push_back(&Points<D>::func2);
    p.execute_all(functionlist);
}

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading