Automatic function result type adaption depending on arg?

Discussion in 'C++' started by Alf P. Steinbach, Mar 7, 2010.

  1. I have e.g.

    template< typename T >
    inline typename T::iterator startOf( T& c )
    {
    return c.begin();
    }

    template< typename T >
    inline typename T::const_iterator startOf( T const& c )
    {
    return c.begin();
    }

    template< typename T, Size N >
    inline T* startOf( T (&a)[N] )
    {
    return a;
    }

    Types from the standard library use 'iterator' and 'const_iterator', but imagine
    some other large set of types TheirOwnWay with e.g. 'Iter' and 'ConstIter', and
    that those types are in numeruous different namespaces.

    How can the functions above be modified so that it's simple to make them work
    also with TheirOwnWay types, e.g. by defining some type trait thing?

    I tried the to me "obvious" customization hook, like

    template< typename T >
    inline typename IterTrait<T>::T startOf( T& c ) ...

    keeping the last overload above as-is, but this failed spectacularly when
    calling startOf(a) where a is a raw array.

    I'd rather avoid using Boost enable_if (or any Boost dependency).


    Cheers,

    - Alf
    Alf P. Steinbach, Mar 7, 2010
    #1
    1. Advertising

  2. Alf P. Steinbach

    Ian Collins Guest

    Alf P. Steinbach wrote:
    > I have e.g.
    >
    > template< typename T >
    > inline typename T::iterator startOf( T& c )
    > {
    > return c.begin();
    > }
    >
    > template< typename T >
    > inline typename T::const_iterator startOf( T const& c )
    > {
    > return c.begin();
    > }
    >
    > template< typename T, Size N >
    > inline T* startOf( T (&a)[N] )
    > {
    > return a;
    > }
    >
    > Types from the standard library use 'iterator' and 'const_iterator', but
    > imagine some other large set of types TheirOwnWay with e.g. 'Iter' and
    > 'ConstIter', and that those types are in numeruous different namespaces.
    >
    > How can the functions above be modified so that it's simple to make them
    > work also with TheirOwnWay types, e.g. by defining some type trait thing?


    Wait for C+0x and use the proposed suffix return type syntax. This has
    been added to solve this problem.

    http://www2.research.att.com/~bs/C 0xFAQ.html#suffix-return

    --
    Ian Collins
    Ian Collins, Mar 7, 2010
    #2
    1. Advertising

  3. * Ian Collins:
    > Alf P. Steinbach wrote:
    >> I have e.g.
    >>
    >> template< typename T >
    >> inline typename T::iterator startOf( T& c )
    >> {
    >> return c.begin();
    >> }
    >>
    >> template< typename T >
    >> inline typename T::const_iterator startOf( T const& c )
    >> {
    >> return c.begin();
    >> }
    >>
    >> template< typename T, Size N >
    >> inline T* startOf( T (&a)[N] )
    >> {
    >> return a;
    >> }
    >>
    >> Types from the standard library use 'iterator' and 'const_iterator',
    >> but imagine some other large set of types TheirOwnWay with e.g. 'Iter'
    >> and 'ConstIter', and that those types are in numeruous different
    >> namespaces.
    >>
    >> How can the functions above be modified so that it's simple to make
    >> them work also with TheirOwnWay types, e.g. by defining some type
    >> trait thing?

    >
    > Wait for C+0x and use the proposed suffix return type syntax. This has
    > been added to solve this problem.
    >
    > http://www2.research.att.com/~bs/C 0xFAQ.html#suffix-return


    Thanks! Learned something new. :) Not sure if will solve problem, though, but
    perhaps.


    Cheers,

    - Alf
    Alf P. Steinbach, Mar 7, 2010
    #3
  4. On Mar 7, 9:33 am, "Alf P. Steinbach" <> wrote:
    > I have e.g.
    >
    >      template< typename T >
    >      inline typename T::iterator startOf( T& c )
    >      {
    >          return c.begin();
    >      }
    >
    >      template< typename T >
    >      inline typename T::const_iterator startOf( T const& c )
    >      {
    >          return c.begin();
    >      }
    >
    >      template< typename T, Size N >
    >      inline T* startOf( T (&a)[N] )
    >      {
    >          return a;
    >      }
    >
    > Types from the standard library use 'iterator' and 'const_iterator', but imagine
    > some other large set of types TheirOwnWay with e.g. 'Iter' and 'ConstIter', and
    > that those types are in numeruous different namespaces.
    >
    > How can the functions above be modified so that it's simple to make them work
    > also with TheirOwnWay types, e.g. by defining some type trait thing?
    >
    > I tried the to me "obvious" customization hook, like
    >
    >      template< typename T >
    >      inline typename IterTrait<T>::T startOf( T& c ) ...
    >
    > keeping the last overload above as-is, but this failed spectacularly when
    > calling startOf(a) where a is a raw array.
    >
    > I'd rather avoid using Boost enable_if (or any Boost dependency).


    Is there some reason not to overload startOf as below? This is
    similar to the way I tend to add operator<<()...

    Cheers,
    Tony

    #include <cassert>
    #include <iostream>
    #include <vector>

    template< typename T >
    inline typename T::iterator startOf( T& c )
    {
    std::cout << "startOf(T& c) returning T::iterator\n";
    return c.begin();
    }

    template< typename T >
    inline typename T::const_iterator startOf( T const& c )
    {
    std::cout << "startOf(const T& c) returning T::const_iterator\n";
    return c.begin();
    }

    template< typename T, size_t N >
    inline T* startOf( T (&a)[N] )
    {
    std::cout << "startOf(T (&a)[N]) returning a\n";
    return a;
    }

    struct X
    {
    typedef const int* Const_Iterator;
    typedef int* Iterator;

    X(int a, int b) { a_[0] = a; a_[1] = b; }

    Const_Iterator first() const { return a_; }
    Iterator first() { return a_; }

    friend Const_Iterator startOf(const X& x) {
    std::cout << "startOf(const X&) returning X::Const_Iterator
    \n";
    return x.first();
    }
    friend Iterator startOf(X& x) {
    std::cout << "startOf(X&) returning X::Iterator\n";
    return x.first();
    }

    int a_[2];
    };

    int main()
    {
    std::vector<int> i;
    i.push_back(4);
    i.push_back(10);
    assert(*startOf(i) == 4);
    const std::vector<int>& ci = i;
    assert(*startOf(ci) == 4);

    int ai[2] = { 4, 10 };
    assert(*startOf(ai) == 4);

    X x(4, 10);
    assert(*startOf(x) == 4);
    const X& cx = x;
    assert(*startOf(cx) == 4);
    }
    Anthony Delroy, Mar 8, 2010
    #4
  5. * Anthony Delroy:
    > On Mar 7, 9:33 am, "Alf P. Steinbach" <> wrote:
    >> I have e.g.
    >>
    >> template< typename T >
    >> inline typename T::iterator startOf( T& c )
    >> {
    >> return c.begin();
    >> }
    >>
    >> template< typename T >
    >> inline typename T::const_iterator startOf( T const& c )
    >> {
    >> return c.begin();
    >> }
    >>
    >> template< typename T, Size N >
    >> inline T* startOf( T (&a)[N] )
    >> {
    >> return a;
    >> }
    >>
    >> Types from the standard library use 'iterator' and 'const_iterator', but imagine
    >> some other large set of types TheirOwnWay with e.g. 'Iter' and 'ConstIter', and
    >> that those types are in numeruous different namespaces.
    >>
    >> How can the functions above be modified so that it's simple to make them work
    >> also with TheirOwnWay types, e.g. by defining some type trait thing?
    >>
    >> I tried the to me "obvious" customization hook, like
    >>
    >> template< typename T >
    >> inline typename IterTrait<T>::T startOf( T& c ) ...
    >>
    >> keeping the last overload above as-is, but this failed spectacularly when
    >> calling startOf(a) where a is a raw array.
    >>
    >> I'd rather avoid using Boost enable_if (or any Boost dependency).

    >
    > Is there some reason not to overload startOf as below? This is
    > similar to the way I tend to add operator<<()...
    >
    > Cheers,
    > Tony
    >
    > #include <cassert>
    > #include <iostream>
    > #include <vector>
    >
    > template< typename T >
    > inline typename T::iterator startOf( T& c )
    > {
    > std::cout << "startOf(T& c) returning T::iterator\n";
    > return c.begin();
    > }
    >
    > template< typename T >
    > inline typename T::const_iterator startOf( T const& c )
    > {
    > std::cout << "startOf(const T& c) returning T::const_iterator\n";
    > return c.begin();
    > }
    >
    > template< typename T, size_t N >
    > inline T* startOf( T (&a)[N] )
    > {
    > std::cout << "startOf(T (&a)[N]) returning a\n";
    > return a;
    > }
    >
    > struct X
    > {
    > typedef const int* Const_Iterator;
    > typedef int* Iterator;
    >
    > X(int a, int b) { a_[0] = a; a_[1] = b; }
    >
    > Const_Iterator first() const { return a_; }
    > Iterator first() { return a_; }
    >
    > friend Const_Iterator startOf(const X& x) {
    > std::cout << "startOf(const X&) returning X::Const_Iterator
    > \n";
    > return x.first();
    > }
    > friend Iterator startOf(X& x) {
    > std::cout << "startOf(X&) returning X::Iterator\n";
    > return x.first();
    > }
    >
    > int a_[2];
    > };
    >
    > int main()
    > {
    > std::vector<int> i;
    > i.push_back(4);
    > i.push_back(10);
    > assert(*startOf(i) == 4);
    > const std::vector<int>& ci = i;
    > assert(*startOf(ci) == 4);
    >
    > int ai[2] = { 4, 10 };
    > assert(*startOf(ai) == 4);
    >
    > X x(4, 10);
    > assert(*startOf(x) == 4);
    > const X& cx = x;
    > assert(*startOf(cx) == 4);
    > }


    Thanks. I was thinking of something that wouldn't require defining each of
    'startOf', 'endOf' and 'size' for each class. But if that's what's practically
    required then OK.


    Cheers,

    - Alf
    Alf P. Steinbach, Mar 8, 2010
    #5
  6. On Mar 8, 6:50 pm, "Alf P. Steinbach" <> wrote:
    > Thanks. I was thinking of something that wouldn't require defining each of
    > 'startOf', 'endOf' and 'size' for each class. But if that's what's practically
    > required then OK.


    Well, macros? :-(

    Good luck,
    Tony
    Anthony Delroy, Mar 9, 2010
    #6
  7. Alf P. Steinbach

    werasm Guest

    On Mar 7, 2:33 am, "Alf P. Steinbach" <> wrote:
    > I have e.g.
    >
    >      template< typename T >
    >      inline typename T::iterator startOf( T& c )
    >      {
    >          return c.begin();
    >      }
    >
    >      template< typename T >
    >      inline typename T::const_iterator startOf( T const& c )
    >      {
    >          return c.begin();
    >      }
    >
    >      template< typename T, Size N >
    >      inline T* startOf( T (&a)[N] )
    >      {
    >          return a;
    >      }
    >
    > Types from the standard library use 'iterator' and 'const_iterator', but imagine
    > some other large set of types TheirOwnWay with e.g. 'Iter' and 'ConstIter', and
    > that those types are in numeruous different namespaces.
    >
    > How can the functions above be modified so that it's simple to make them work
    > also with TheirOwnWay types, e.g. by defining some type trait thing?
    >
    > I tried the to me "obvious" customization hook, like
    >
    >      template< typename T >
    >      inline typename IterTrait<T>::T startOf( T& c ) ...
    >
    > keeping the last overload above as-is, but this failed spectacularly when
    > calling startOf(a) where a is a raw array.


    I came up with the code below. CProxy would have to be specialized for
    non standard containers (as for array). Unfortunately one needs the
    extra call to c_proxy function template to resolve T, but IMO bettern
    than specifying the template parameter:

    #include <vector>

    template <class T>
    struct CProxy
    {
    typedef typename T::iterator iterator;
    typedef T& ContRefT;

    explicit CProxy( ContRefT c ): c_( c ){}
    ContRefT c_;
    };

    template <class T, int N>
    struct CProxy<T(&)[N]>
    {
    typedef T* iterator;
    typedef T(&ContRefT)[N];

    struct ArrayWrapper
    {
    ArrayWrapper( ContRefT c ): c_( c ){}
    iterator begin() const { return &c_[0]; }
    //...end etc...
    ContRefT c_;
    };

    explicit CProxy( ContRefT c ): c_( c ){}
    ArrayWrapper c_;
    };

    template <class ContT>
    CProxy<ContT> c_proxy( ContT c )
    {
    return CProxy<ContT>( c );
    }
    template <class T, int N>
    CProxy<T(&)[N]> c_proxy( T(&a)[N] )
    {
    return CProxy<T(&)[N]>( a );
    }


    template< typename T >
    inline typename CProxy<T>::iterator
    startOf( CProxy<T> cproxy )
    {
    return cproxy.c_.begin();
    }

    int main()
    {
    char array[5];
    char* a =
    startOf( c_proxy(array) );

    const std::vector<char> vect;
    std::vector<char>::iterator b =
    startOf( c_proxy(vect) );

    }


    Kind regards,

    Werner





    >
    > I'd rather avoid using Boost enable_if (or any Boost dependency).
    >
    > Cheers,
    >
    > - Alf
    werasm, Apr 13, 2010
    #7
  8. * werasm:
    > On Mar 7, 2:33 am, "Alf P. Steinbach" <> wrote:
    >> I have e.g.
    >>
    >> template< typename T >
    >> inline typename T::iterator startOf( T& c )
    >> {
    >> return c.begin();
    >> }
    >>
    >> template< typename T >
    >> inline typename T::const_iterator startOf( T const& c )
    >> {
    >> return c.begin();
    >> }
    >>
    >> template< typename T, Size N >
    >> inline T* startOf( T (&a)[N] )
    >> {
    >> return a;
    >> }
    >>
    >> Types from the standard library use 'iterator' and 'const_iterator', but imagine
    >> some other large set of types TheirOwnWay with e.g. 'Iter' and 'ConstIter', and
    >> that those types are in numeruous different namespaces.
    >>
    >> How can the functions above be modified so that it's simple to make them work
    >> also with TheirOwnWay types, e.g. by defining some type trait thing?
    >>
    >> I tried the to me "obvious" customization hook, like
    >>
    >> template< typename T >
    >> inline typename IterTrait<T>::T startOf( T& c ) ...
    >>
    >> keeping the last overload above as-is, but this failed spectacularly when
    >> calling startOf(a) where a is a raw array.

    >
    > I came up with the code below. CProxy would have to be specialized for
    > non standard containers (as for array). Unfortunately one needs the
    > extra call to c_proxy function template to resolve T, but IMO bettern
    > than specifying the template parameter:
    >
    > #include <vector>
    >
    > template <class T>
    > struct CProxy
    > {
    > typedef typename T::iterator iterator;
    > typedef T& ContRefT;
    >
    > explicit CProxy( ContRefT c ): c_( c ){}
    > ContRefT c_;
    > };
    >
    > template <class T, int N>
    > struct CProxy<T(&)[N]>
    > {
    > typedef T* iterator;
    > typedef T(&ContRefT)[N];
    >
    > struct ArrayWrapper
    > {
    > ArrayWrapper( ContRefT c ): c_( c ){}
    > iterator begin() const { return &c_[0]; }
    > //...end etc...
    > ContRefT c_;
    > };
    >
    > explicit CProxy( ContRefT c ): c_( c ){}
    > ArrayWrapper c_;
    > };
    >
    > template <class ContT>
    > CProxy<ContT> c_proxy( ContT c )
    > {
    > return CProxy<ContT>( c );
    > }
    > template <class T, int N>
    > CProxy<T(&)[N]> c_proxy( T(&a)[N] )
    > {
    > return CProxy<T(&)[N]>( a );
    > }
    >
    >
    > template< typename T >
    > inline typename CProxy<T>::iterator
    > startOf( CProxy<T> cproxy )
    > {
    > return cproxy.c_.begin();
    > }
    >
    > int main()
    > {
    > char array[5];
    > char* a =
    > startOf( c_proxy(array) );
    >
    > const std::vector<char> vect;
    > std::vector<char>::iterator b =
    > startOf( c_proxy(vect) );
    >
    > }


    Thank you. I should perhaps have remembered to post my solution to this thread.
    My original error, which was almost driving me nuts in its sheer ingrokkability,
    was a trivial one, incredibly stupid, writing 'T (&)[N]' instead of 'T [N]'.

    I'm sure if I'd posted that code many folks would have been better able to help
    me; as it was they probably assumed that my code made sense!

    Anyway, the solution I landed on consists of two header files, the first
    containing ...


    <code>
    template< typename Type >
    struct It
    {
    typedef typename Type::iterator T;
    };

    template< typename Type >
    struct It< Type const >
    {
    typedef typename Type::const_iterator T;
    };

    template< typename ElemType, Size N >
    struct It< ElemType[N] >
    {
    typedef ElemType* T;
    };
    </code>


    .... and the second one, using the first, containing ...


    <code>
    template< typename T >
    inline Size size( T const& c ) { return static_cast<Size>(
    c.size() ); }

    template< typename T, Size N >
    inline Size size( T (&)[N] ) { return N; }

    template< typename T >
    inline typename It<T>::T startOf( T& c ) { return c.begin(); }

    template< typename T, Size N >
    inline T* startOf( T (&a)[N] ) { return a; }

    template< typename T >
    inline typename It<T>::T endOf( T& c ) { return c.end(); }

    template< typename T, Size N >
    inline T* endOf( T (&a)[N] ) { return a + N; }


    #define CPPX_ALL_OF( v ) \
    ::my_namespace::startOf( v), ::my_namespace::endOf( v )
    </code>


    Cheers, & thanks,

    - Alf (master of writing extraneous ampersands)
    Alf P. Steinbach, Apr 14, 2010
    #8
    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.
Similar Threads
  1. n00m
    Replies:
    5
    Views:
    385
  2. Michael Tan
    Replies:
    32
    Views:
    932
    Ara.T.Howard
    Jul 21, 2005
  3. Tammo Tjarks
    Replies:
    0
    Views:
    93
    Tammo Tjarks
    Feb 7, 2007
  4. Replies:
    7
    Views:
    162
  5. Replies:
    21
    Views:
    274
    Barry Schwarz
    Mar 5, 2014
Loading...

Share This Page