Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-01-21 13:41:53


I've been thinking about this a bit, and I think it can be done in some
cases. A few limitations apply:

    1. You have to give up on argument-dependent lookup. That's not really
such a big deal if I understand correctly, because partial ordering of
function templates doesn't take effect until after the namespace of viable
functions is located.

    2. You need to write your generic functions in a slightly different
style, using pairs of arguments. The first argument in a pair, whose value
is unused, is just there for concept matching. The second argument is the
actual value you're interested in passing. For example:

    template <class InputIterator, class OutputIterator>
    void copy(input_iterator, InputIterator start
            , input_iterator, InputIterator finish
            , output_iterator, OutputIterator result)
    {

    }

The library provides the dispatching function:

    template <class X, class Y>
    void copy(X x1, X x2, Y y)
    {
        copy(typename concept<X>::type(), x1
            , typename concept<X>::type(), x2
            , typename concept<Y>::type(), y);
    }

Of course, concept<T> is a traits class which serves up a type tag
corresponding to the trait. I think the overload resolution rules will tend
to choose the closest matching implementation.

Things get a lot more interesting when you have return values involved. At
that point we need __typeof__():

    template <class X, class Y>

    __typeof__(copy(typename concept<X>::type(), *((X*)())
            , typename concept<X>::type(), *((X*)())
            , typename concept<Y>::type(), *((Y*)()))
    void copy(X x1, X x2, Y y)
    {
        return copy(typename concept<X>::type(), x1
            , typename concept<X>::type(), x2
            , typename concept<Y>::type(), y);
    }

Fortunately, you only need to write one of these ugly dispatching functions.

-Dave

----- Original Message -----
From: "Ralf W. Grosse-Kunstleve" <rwgk_at_[hidden]>
To: <boost_at_[hidden]>
Cc: <rwgk_at_[hidden]>
Sent: Sunday, January 20, 2002 8:33 AM
Subject: [boost] Classification of template typename labels?

> (This question is related to my previous posting.)
>
> Here is something I am often wondering about:
>
> template <typename NumVectorType>
> NumVectorType operator*(const NumVectorType& lhs, const NumVectorType&
rhs);
>
> Is there a way to classify or register types as NumVectorTypes?
>
> I know how to make a distinction between types once I am
> /inside/ the function (using traits). However, what I am
> after is something like:
>
> tell_compiler {
> possible typename NumType:
> double
> float
> etc.
> possible typename NumVectorType:
> std::vector<NumType>
> boost::array<NumType, *>
> etc.
> }
>
> Then the compiler should select template functions only if the
> concrete type is registered under the typename label.
>
> Is this asking for a formalization of "concepts?"
> To which degree can this be emulated with the language as-is?
> What can we expect in the future (from the standard committee)?
>
> Thanks,
> Ralf
>
> Info: http://www.boost.org Send unsubscribe requests to:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


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