Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the type of parameters passed in a variable arguments list

I have written a function with variable argument list

void cvShowMatImages( char* title, int nArgs, ...)  // Mat Images

where the argument to pass are openCV images. I have actually 2 different functions for the 2 image formats IplImage and Mat, the above mentioned and a second one

void cvShowIplImages( char* title, int nArgs, ...)  // Ipl Images

But I cannot mix images of the 2 types. I could resolve my issue if I would be able to determine the type of argument passed, but I do not know how to do. This is how I read the argument:

// Get the images passed as arguments
va_list args;
// Initialize the variable argument list
va_start( args, nArgs );
// Loop on each image
for ( int num = 0; num < nArgs; num++ )
{
   // Get the image to be copied from the argument list
   srcImg = va_arg( args, Mat );
   ...

and for IplImage:

srcImg = va_arg( args, IplImage* );

In both case srcImg is declared as

Mat srcImg

since there is an overloaded operator= for IplImage. Is there a way to solve this issue?

like image 321
Alain Weiler Avatar asked Nov 13 '22 03:11

Alain Weiler


1 Answers

Using variadic templates in this manner is a possible solution:

#include <iostream>

template<typename... Args> void func(double t, Args... args) ;
template<typename... Args> void func(int t, Args... args) ;

void func(int t) 
{
    std::cout << "int: "<< t << std::endl ;
}

void func(double t) 
{
    std::cout << "double: "<< t << std::endl ;
}

template<typename... Args>
void func(double t, Args... args) 
{
    func(t) ;
    func(args...) ;
}

template<typename... Args>
void func(int t, Args... args) 
{
    func(t) ;
    func(args...) ;
}


int main()
{
    int x1 = 1, x2 = 5 ;
    double d1 = 2.5 , d2 = 3.5;

    func( x1 , d1, x2 ) ;
    func( x1 , d1, d2 ) ;
} 

It is not very elegant but it may help solve your problem. Another method would be to use two std::initializer_list one for each type, like so:

#include <iostream>
#include <initializer_list>

void func2( std::initializer_list<int> listInt, std::initializer_list<double> listDouble )
{
    for( auto elem : listInt )
    {
        std::cout << "Int: " << elem << std::endl ;
    }

    for( auto elem : listDouble )
    {
        std::cout << "double: " << elem << std::endl ;
    }
}

int main()
{
    func2( {10, 20, 30, 40 }, {2.5, 2.5 }) ;    
} 
like image 128
Shafik Yaghmour Avatar answered Nov 14 '22 22:11

Shafik Yaghmour