Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 1999-11-25 13:44:47

Beman Dawes wrote on 11/23/99 9:31 PM
>I also tried specializing on <T[sz]> and <const T[sz]> like this:
>template< typename T, int sz >
>struct parameter_traits< const T[sz] >
> typedef const T * decayed_type;
> typedef const T * parameter_type;
> static const char * which() { return "const T[sz]
>specialization"; }
>This actually works, at least with GCC 2.95.2! Many other compilers
>decay the template argument, which they shouldn't, according to
>Dietmar Kuehl.

Could you send me a complete HelloWorld with this? I'm working with an
alpha compiler that's supposed to be fixed to work with Dietmar's trick,
but I'm having trouble with this one. Thanks.

John Maddock wrote on 11/25/99 7:55 AM
>but if foo can be inlined, and iterator is a class type rather than a
>pointer, then we lose effeciency because inline expansion will be
>prohibited, so:

I didn't understand this part. Why would inline expansion be prohibited
when iterator is a class? My compiler will inline the following call to

#include <list>
#include <numeric>

int main()
        std::list<int> a;
        int sum = std::accumulate(a.begin(), a.end(), 0);

>template <class iterator>
>void foo(typename parameter_traits<iterator>::parameter_type i, typename
>parameter_traits<iterator>::parameter_type j){ /*...*/ }
>Unfortunately, having experimented with this, template argument deduction
>fails in such cases - and thinking about it I can't see how it could have
>succeeded, specifying the template parameters explicitly works (as you
>would expect), but this isn't really much use - so it looks like the
>technique is limited to member functions.

I think there's still possibility here. Many such foo will look like

template <class iterator>
foo(iterator i, iterator j)
     for (; i != j; ++i)
     { ... }

So passing i by const& wouldn't gain anything anyway as you would have to
make a copy of it. So only j would possibly gain from this optimization.
 And this seems to work:

template <class iterator>
foo(iterator i, typename parameter_traits<iterator>::parameter_type j)
        for (; i != j; ++i)

Agreed that if foo() has only one argument, we seem to be out of luck.


Boost list run by bdawes at, gregod at, cpdaniel at, john at