LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-20-2019, 09:10 AM   #1
rdx
Member
 
Registered: Apr 2007
Location: Dallas
Distribution: Slackware64 14.2
Posts: 283

Rep: Reputation: 25
C++: passing list of functions as argument to function


This is really a pure C++ question (g++).
I want to pass a list of functions to another function so that it can call those in the list in sequence and accumulate their outputs in a matrix. The functions in the list are "double (*func)( vec x)" in the syntax of the Armadillo linear algebra library.

This is similar I think to the way hardware drivers used to have a list of function calls to access "read," "write," and control functions but I haven't seen that usage in a long time.

What I am trying to do is pass the pointers as "uwords" since that is a type acceptable to Armadillo and then cast the unsigned long ints to function calls. If this seems too awkward I would fall back on a STL <vector> to contain the list. Other ideas welcomed. The key feature here is that the number of functions in the list is arbitrary.

Last edited by rdx; 10-20-2019 at 09:39 AM.
 
Old 10-20-2019, 06:07 PM   #2
rdx
Member
 
Registered: Apr 2007
Location: Dallas
Distribution: Slackware64 14.2
Posts: 283

Original Poster
Rep: Reputation: 25
After a few hours study I have found the magic syntax.
First I use Armadillo "uvec" type (a column vector of "uword" or unsigned long long int), I cast the functions to these uwords and then cast them back in the called function.

Code:
#include <armadillo>
using namespace arma ;

double f1( vec x ) ;
double f2( vec x ) ;
double f3( vec x ) ;

int main()
{
    vec  x = ones<vec>(3) ;
    uvec U ;
    U << (uword) f1 << (uword) f2 << (uword) f3 ;

    vec V = my_function( U, x ) ;
       ----
}

vec my_function( uvec U, vec x )
{
    int M = U.n_item ;
    double  (*func)(vec) = (double (*)(vec)) U(0) ;
    double y = func(x) ;
        ....
}
It appears that the STL <vector> will also work.
Code:
#include <vector>
using namespace std ;

int main()
{
       ....
    vector<double (*)(arma::vec)>  V ;
    V.push_back( f1 ) ;
    V.push_back( f2 ) ;
    V.push_back( f3 ) ;
    arma::mat J = my_function( V, x ) ;
       ....
}

arma::mat my_function( vector<double (*)(arma::vec)> V, 
          arma::vec x )
{
    double (*func)(arma::vec) = V.at(0) ;
       ....
}
Personally I prefer using the Armadillo uvec but the g++ compiler is accepting both approaches.

Last edited by rdx; 10-20-2019 at 06:49 PM.
 
  


Reply

Tags
g++ armadillo



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing double pointer to function to return value from function as argument srinietrx Programming 2 10-06-2019 12:55 AM
Getting Error While Passing Structure as a function Argument in C ? findnerd2 Programming 7 06-20-2015 03:04 AM
C passing a Array of Structuers as an argument in a function dman777 Programming 5 08-22-2011 06:16 PM
C++ passing variable argument list to other functions R00ts Programming 8 08-08-2008 05:10 PM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration