Variable number of arguments

Function overloading

CliPP has several ways of handling the case where the user wants a function with a given name to accept different number of arguments.
The simplest is overloading:

    void foo_1(int a);
    void foo_2(int a,int b);

Which when wrapped becomes:

    cls.function("foo",foo_1);
    cls.function("foo",foo_2);

Note that in the above example, we need, in C++, the function names to be unambiguous for the compiler to know which function pointer to use in the cls.function(...) function. When two functions share the same name in C++, the only thing that distinguishes them are their function signatures. Given the above example:

    void foo(int a);
    void foo(int a,int b);

If we want to expose either version of the foo function to CliPP, we need to include the full function signature in order to make the choice of function pointer unambiguous.

//Preferred
    cls.function("foo",(void (*)(int))foo);
    cls.function("foo",(void (*)(int,int))foo);

//Or, for compatibility:
    cls["foo"] = function<void (*)(int)>(foo);
    cls["foo"] = function<void (*)(int,int)>(foo);

For constructors, we need to explicitly specify all the arguments to be passed to the constructor anyway, because constructors are not proper functions (You can not access them via function pointers, hence you can not deduce the arguments required for a constructor)

To overload a constructor, simply expose two constructors with different signature:

//Define class:
    class test {
	test(int a);
	test(int a,int b);
    };
//Expose constructors:
    cls[constructor<int>()];
    cls[constructor<int,int>()];

Varargs (...)

CliPP supports variable number of arguments in functions and constructors. In C++, you specify that you want to use this feature by letting the last argument of your function be of type valarray<T>.  Where T is the type you want the additional arguments to have. The most general type is value* (defined in boost::clipp) which will accept all CliPP types. An example of its use is in the javascript function Math.max(...)

    //Define constructors	
    double math::max(const std::valarray<double>& args);
    ...
    //Exposing to CliPP
    cls.function("max",max);
    ...
    //Using in Javascript
    var a=Math.max(5,6,9.81);
    //This will produce an error message, because "Hello" can not be converted to double
    var b=Math.max(6,8,"Hello");

Default arguments

    //A function argument given a default value	
    void foo(int a,int b=0);

All functions in CliPP are transferred using function pointers. Unfortunately, C++ function pointers does not contain information about the default value specified. There is no way to extract this information compile time from a function pointer. There are two ways to solve this problem, either by creating (in the above example) a dummy function with one argument that calls the function with two arguments or you can explicitly specify the optional arguments when exposing the function to CliPP:

    //A function argument given a default value	
    cls.function("foo",foo).signature("a",arg("b")=0);

The above method works for both functions and constructors.