Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable parameter function, how to make it type safe and more meaningful?

Tags:

c++

I am a newer for C++, and my first language is Chinese, so my words with English may be unmeaningful, say sorry first. I know there is a way to write a function with variable parameters which number or type maybe different each calling, we can use the macros of va_list,va_start and va_end. But as everyone know, it is the C style. When we use the macros, we will lose the benefit of type-safe and auto-inference, then I try do it whit C++ template. My work is followed:


#include<iostream>
#include<vector>
#include<boost/any.hpp>

struct Argument
{
    typedef boost::bad_any_cast bad_cast;

    template<typename Type>
    Argument& operator,(const Type& v)
    {
        boost::any a(v);
        _args.push_back(a);
        return *this;
    }

    size_t size() const
    {
        return _args.size();
    }

    template<typename Type>
    Type value(size_t n) const
    {
        return boost::any_cast<Type>(_args[n]);
    }

    template<typename Type>
    const Type* piont(size_t n) const
    {
        return boost::any_cast<Type>(&_args[n]);
    }
private:
    std::vector<boost::any> _args;
};

int sum(const Argument& arg)
{
    int sum=0;
    for(size_t s=0; s<arg.size(); ++s)
    {
        sum += arg.value<int>(s);
    }

    return sum;
}

int main()
{
    std::cout << sum((Argument(), 1, 3, 4, 5)) << std::endl;

    return 0;
}

I think it's ugly, I want to there is a way to do better? Thanks, and sorry for language errors.

like image 375
easyeagel Avatar asked Aug 29 '09 06:08

easyeagel


People also ask

Which function is used for variable parameters?

Here we use macros to implement the functionality of variable arguments. Use va_list type variable in the function definition. Use int parameter and va_start macro to initialize the va_list variable to an argument list.

How do you use variable arguments in C++?

Variable number of arguments in C++Define a function with its last parameter as ellipses and the one just before the ellipses is always an int which will represent the number of arguments. Create a va_list type variable in the function definition. This type is defined in stdarg. h header file.

Can a function have variable number of parameters justify?

You can define your own methods with variable parameters, and you can specify any type for the parameters, even a primitive type. Here is a simple example: a function that computes the maximum of a variable number of values. Simply call the function like this: double m = max(3.1, 40.4, -5);

What is Variadic function C++?

Variadic functions are functions (e.g. printf) which take a variable number of arguments. The declaration of a variadic function uses an ellipsis as the last parameter, e.g. int printf(const char* format, ...);. See variadic arguments for additional detail on the syntax and automatic argument conversions.


1 Answers

You can do something like this:

template <typename T>
class sum{
    T value;
    public:
    sum ()
            : value() {};
    // Add one argument
    sum<T>& operator<<(T const& x)
            { value += x; return *this; }
    // to get funal value
    operator T()
            { return value;}
    // need another type that's handled differently?  Sure!
    sum<T>& operator<<(double const& x)
            { value += 100*int(x); return *this; }
};

#include <iostream>

int main()
{
    std::cout << (sum<int>() << 5 << 1 << 1.5 << 19) << "\n";
    return 0;
}

Such technique (operator overloading and stream-like function class) may solve different problems with variable arguments, not only this one. For example:

create_window() << window::caption - "Hey" << window::width - 5;
     // height of the window and its other parameters are not set here and use default values
like image 90
P Shved Avatar answered Sep 27 '22 22:09

P Shved