Boost logo

Boost :

From: Beman Dawes (beman_at_[hidden])
Date: 1999-11-27 09:34:53


At 11:38 AM 11/26/99 -0500, Howard Hinnant wrote:

>Perhaps a twist to automate many of the parameter_traits
specializations?
>
> ...

Wow! This is a really neat idea!

I didn't understand all of your comments, so put together the test
program below.

It works like a charm with CodeWarrior 5.2. See the last two test
cases in particular.

(It causes GCC 2.95.2 to loop; if anyone has a workaround please post
it here.)

Note name changes and typedef simplification. I personally feel they
are an improvement, but nothing is finalized yet.

--Beman

----- snip here ----

// Explore Howard's automated call_traits idiom

#include <iostream>
#include <typeinfo>
#include <vector>
using namespace std;

template< typename T, bool IsSmall = sizeof(T) <= sizeof(void*) >
struct
call_traits;

template< typename T >
struct call_traits< T, false >
{
        typedef T const & type;
        static const char * which() { return "reference"; }
};

template< typename T >
struct call_traits< T, true >
{
        typedef T const type;
        static const char * which() { return "value"; }
};

template< typename T >
struct single
{
    typename T value;
    single( typename call_traits<T>::type v ) : value(v)
    {
        cout << "Constructing " << typeid(value).name()
             << " via pass-by-" << call_traits<T>::which()
             << endl;
    }
    virtual ~single(){}
};

int main()
{
    double d = 1.234;
    single<double> d1(d);
    single<double*> d2(&d);
    single<const double*> d3(&d);
    single<double&> d4(d);
    single<const double&> d5(d);
    vector<int> vi;
    single< vector<int> > svi(vi);
    single< vector<int>::iterator > vii(vi.begin());
    return 0;
}

---- end ----


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