Template that can take variable number of arguments

Discussion in 'C++' started by Ganny, Apr 6, 2005.

  1. Ganny

    Ganny Guest

    Is it possible to write a template function min that takes variable
    number of arguments? It should be without using ugly C var args,
    arrays, or statically overloading the method for N arguments. Say, min
    function which will take N arguments. Any approaches like generative
    programming will help?

    -Ganesh
    Ganny, Apr 6, 2005
    #1
    1. Advertising

  2. Ganny wrote:
    > Is it possible to write a template function min that takes variable
    > number of arguments? It should be without using ugly C var args,
    > arrays, or statically overloading the method for N arguments. Say, min
    > function which will take N arguments. Any approaches like generative
    > programming will help?


    Just overload the min. The number of arguments is essentially one of
    the type traits of the function.

    V
    Victor Bazarov, Apr 6, 2005
    #2
    1. Advertising

  3. Ganny

    ulrich Guest

    On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:

    > Is it possible to write a template function min that takes variable
    > number of arguments? It should be without using ugly C var args,
    > arrays, or statically overloading the method for N arguments. Say, min
    > function which will take N arguments. Any approaches like generative
    > programming will help?


    if you insist on searching the minimum yourself, my suggestion would be to
    pass a std::vector<> to your function. your function can then easily
    iterate through the vector and search the minimum.

    however, there is the class std::valarray<>, featuring the method - guess
    what - std::valarray<>::min()
    ulrich, Apr 6, 2005
    #3
  4. ulrich wrote:
    > On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:
    >
    >> Is it possible to write a template function min that takes variable
    >> number of arguments? It should be without using ugly C var args,
    >> arrays, or statically overloading the method for N arguments. Say, min
    >> function which will take N arguments. Any approaches like generative
    >> programming will help?

    >
    >
    > if you insist on searching the minimum yourself, my suggestion would be
    > to pass a std::vector<> to your function. your function can then
    > easily iterate through the vector and search the minimum.


    From that point of view, a pair of iterators is much more preferable.

    > however, there is the class std::valarray<>, featuring the method -
    > guess what - std::valarray<>::min()


    V
    Victor Bazarov, Apr 6, 2005
    #4
  5. Ganny

    ?? Guest

    "Ganny" <> ???????/???????? ? ???????? ?????????:
    news:...
    > Is it possible to write a template function min that takes variable
    > number of arguments? It should be without using ugly C var args,
    > arrays, or statically overloading the method for N arguments. Say, min
    > function which will take N arguments. Any approaches like generative
    > programming will help?
    >
    > -Ganesh
    >


    Another suggestion would be to pass one at a
    time,just like Andrei Alexandrescu suggests in
    one of his articles,"Inline Containers".Just
    imagine how standard streams handle this:
    namespace manyargs{
    template <typename T>
    class minfunc
    {
    public:
    minfunc(const T& x):ptr(&x){}

    minfunc& operator() (const T& x)
    {
    if(*ptr_>x) ptr_=&x;
    return *this;
    }

    const T& operator() ()
    {
    return *ptr_;
    }
    private:
    const T* ptr_;
    };

    template <typename T>
    minfunc<T> min(const T& x)
    {
    return minfunc<T>(x);
    }
    }
    And use it like this:

    int a=manyargs::min(123)(56)(788)(777)();

    I agree that the syntax is somewhat ugly,but
    still completely typesafe, as opposed to printf
    and their kin.
    ??, Apr 6, 2005
    #5
  6. Ganny wrote:

    > Is it possible to write a template function min that takes variable
    > number of arguments? It should be without using ugly C var args,
    > arrays, or statically overloading the method for N arguments. Say, min
    > function which will take N arguments. Any approaches like generative
    > programming will help?



    You could pass in it a std::pair of pairs. However I think Boost provides more elegant
    solutions:

    http://www.boost.org


    Just download and set it up (it is easy) for your compiler.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Apr 6, 2005
    #6
  7. Ganny

    Evan Guest

    > if you insist on searching the minimum yourself, my suggestion would
    be to
    > pass a std::vector<> to your function. your function can then easily


    > iterate through the vector and search the minimum.


    But then you have to put stuff in an array. I think he's looking for
    something you could call like:

    min(x, y, z, 10, k, g.getValue());


    The answer to this question is yes and no. I would suggest, as victor
    did, writing just a series of overloaded functions min(a,b),
    min(a,b,c), etc., possibly with each function referring to the last.
    For instance:

    template<typename T>
    T min(T a, T b, T c, T d)
    {
    T maybeMin = min(a,b,c);

    if(maybeMin < d)
    return maybeMin;
    else
    return d;
    }

    Make it up to 8 arguments or whatever you need.

    There was a proposal before the C++ standards committee for type-safe,
    variable length function parameters to replace the '...' in functions
    such as printf. If accepted, this would provide a nicer way of doing
    this. However, it won't be for a while; it'd need to be accepted, added
    to the next version of the standard in who-knows-when, then implemented
    before you could use it, so it's still a number of years off.
    Evan, Apr 6, 2005
    #7
  8. Ganny

    ulrich Guest

    On Wed, 06 Apr 2005 10:47:42 -0400, Victor Bazarov
    <> wrote:

    > ulrich wrote:
    >> On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:
    >>
    >>> Is it possible to write a template function min that takes variable
    >>> number of arguments? It should be without using ugly C var args,
    >>> arrays, or statically overloading the method for N arguments. Say, min
    >>> function which will take N arguments. Any approaches like generative
    >>> programming will help?

    >> if you insist on searching the minimum yourself, my suggestion would
    >> be to pass a std::vector<> to your function. your function can then
    >> easily iterate through the vector and search the minimum.

    >
    > From that point of view, a pair of iterators is much more preferable.


    in order to get independent from the type of the container?
    ulrich, Apr 7, 2005
    #8
  9. ulrich wrote:
    > On Wed, 06 Apr 2005 10:47:42 -0400, Victor Bazarov
    > <> wrote:
    >
    >> ulrich wrote:
    >>
    >>> On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:
    >>>
    >>>> Is it possible to write a template function min that takes variable
    >>>> number of arguments? It should be without using ugly C var args,
    >>>> arrays, or statically overloading the method for N arguments. Say, min
    >>>> function which will take N arguments. Any approaches like generative
    >>>> programming will help?
    >>>
    >>> if you insist on searching the minimum yourself, my suggestion
    >>> would be to pass a std::vector<> to your function. your function
    >>> can then easily iterate through the vector and search the minimum.

    >>
    >>
    >> From that point of view, a pair of iterators is much more preferable.

    >
    >
    > in order to get independent from the type of the container?


    Absolutely.
    Victor Bazarov, Apr 7, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page