1

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.

5
  • What type of argument are you passing to the execute_all function? What is the type of argument it expects? Is the code you show us really a proper minimal reproducible example? Commented Jul 9, 2023 at 17:06
  • On a different note, get off the habit of passing containers as argument, and use iterators instead. Commented Jul 9, 2023 at 17:07
  • 1
    it must be template<int D>using fptr = void (Points<D>::*)(); and your function expect a vector and receive a set, you must choose Commented Jul 9, 2023 at 17:11
  • @Someprogrammerdude could you give a one line code of how to pass iterators as argument in this scenario? If I use container reference, would that be better? Commented Jul 9, 2023 at 17:18
  • 1
    template<It> void fun(It first, It end)? Just like any function in the standard library. Commented Jul 9, 2023 at 17:58

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0
#include <iostream>
#include <set>
#include <functional>

// Sample template class
template <typename T>
class MyClass {
public:
// Example member function
void foo(T value) {
    std::cout << "foo: " << value << std::endl;
}
};

// Function that accepts a set of member function pointers
template <typename T>
void processMemberFunctionPointers(const std::set<void (MyClass<T>::*)(T)>& 
functionPointers, T value) {
MyClass<T> obj;

// Invoke each member function pointer in the set
for (auto functionPointer : functionPointers) {
    // Call the member function using the object
    (obj.*functionPointer)(value);
}
}

int main() {
// Create a set of member function pointers
std::set<void (MyClass<int>::*)(int)> functionPointers;

// Add member function pointers to the set
functionPointers.insert(&MyClass<int>::foo);

// Process the member function pointers
processMemberFunctionPointers(functionPointers, 42);

return 0;
}

Now, In this example, we have a template class MyClass with a member function foo. The processMemberFunctionPointers function accepts a set of member function pointers as its first parameter. It takes an additional parameter value which is passed to each member function during invocation.

In the main function, we create a set functionPointers to store the member function pointers of MyClass's member functions. We add a member function pointer &MyClass::foo to the set.

Finally, we call processMemberFunctionPointers with the set of member function pointers and a value of 42. The function iterates over each member function pointer in the set and invokes it on an instance of MyClass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.