Boost logo

Boost :

From: Marco (mrcekets_at_[hidden])
Date: 2007-04-16 10:33:23


I gave a glance to the papers about variadic templates and I didn't see
examples using a construct such as f(args)... where args is a function
parameter pack; so I guess that the following code it's illegal:

template< typename T >
T max( T x )
{
   return x;
}

template< typename T, typename... Args >
// where are_same<T, Args...>
T max( T x, T y, Args... args )
{
   return std::max( x, max( y, args... ) );
}

// #1
template< typename T, typename... Args >
// where are_same<T, Args...>
T max_abs( T x, T y, Args... args )
{
   // this is the (probably) illegal construct:
   return max( abs(x), abs(y), abs(args)... );
   // I'd like that "abs(args)..." expands to "abs(arg1), ... ,abs(argN)"
}

What's the rationale behind such a design choice ?

I admit that a simple workaround exists:

// #2
template< typename T >
T max_abs( T x )
{
   return abs(x);
}

template< typename T, typename... Args >
// where are_same<T, Args...>
T max_abs( T x, T y, Args... args )
{
    return std::max(abs(x), max_abs(y, args...) );
}

anyway version #1 is more straightforward, and maybe it's possible to make
up examples more complex than mine.

Another question: given the following functions:

// #1
template< typename T >
void f(T ) {}

// #2
template< typename T, typename... Args >
void f(T, Args... ) {}

the function call f(1,2) is it ambiguous ?
if it isn't, which is the function called ?
#1 because it's more specialized ? (only a guess)

Kind Regards,
Marco

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk