Template partial specialization

Discussion in 'C++' started by Misiu, Mar 17, 2009.

  1. Misiu

    Misiu Guest

    Hello,

    How to write partial specialization for my template class (see code
    bellow) for std::vector<T>::iterator?

    Regards,
    Misiu

    template <typename T>
    struct test
    { static const bool value = false; };

    template <typename T>
    // For vector itself is OK
    struct test<std::vector<T> >

    //but not for its iterator: expected a type, got ‘std::vector::iterator’
    struct test<std::vector<T>::iterator >
    { static const bool value = true; };
     
    Misiu, Mar 17, 2009
    #1
    1. Advertising

  2. Misiu

    SG Guest

    On 17 Mrz., 10:02, Misiu <> wrote:
    > template <typename T>
    > struct test
    > { static const bool value = false; };
    >
    > template <typename T>
    > // For vector itself is OK
    > struct test<std::vector<T> >
    >
    > //but not for its iterator: expected a type, got ‘std::vector::iterator’
    > struct test<std::vector<T>::iterator >
    > { static const bool value = true; };


    Think about it. How can the compiler check whether your type
    parameter matches std::vector<T>::iterator for some unknown T? In
    case of std::vector this may seem trivial but std::vector is an
    ordinary class template which could have looked like this:

    template <typename T>
    class vector { typedef int iterator; };

    If you then explicitly instantiate a class via

    template test<int>;

    what kind of class should be instantiated? In this case
    int==vector<T>::iterator for ALL possible types T. The problem here
    is deducing T or in other words inverting the meta function that maps
    type T -> type std::vector<T>::iterator. Also, this wouldn't be
    really a specialization since std::vector<T>::iterator could be
    basically anything you want it to be via specialization of
    std::vector.

    Cheers!
    SG
     
    SG, Mar 17, 2009
    #2
    1. Advertising

  3. Misiu

    SG Guest

    On 17 Mrz., 10:02, Misiu <> wrote:
    > Hello,
    >
    > How to write partial specialization for my template class (see code
    > bellow) for std::vector<T>::iterator?
    >
    > Regards,
    > Misiu


    You may want to rethink your design.

    Anyhow, with a bit of meta-programming you could achieve a similar
    effect.

    template<bool B, typename T1, typename T2>
    struct if_ { typedef T1 type; };
    template<typename T1, typename T2>
    struct if_<false,T1,T2> { typedef T2 type; };

    template<typename T1, typename T2>
    struct same_ { static const bool value = false; };
    template<typename T>
    struct same_<T,T> { static const bool value = true; };

    template<typename T>
    class impl_default {
    // ...
    protected:
    ~default_impl() {}
    };

    template<typename T>
    class impl_for_vect_iter {
    // ...
    protected:
    ~spec_for_vect_iter() {}
    };

    template<typename T>
    class test : public if_<
    same_<T,typename std::vector<T>::iterator>::value,
    impl_for_vect_iter<T>,
    impl_default<T>
    > {};


    or something like this. (I didn't test it)

    Cheers!
    SG
     
    SG, Mar 17, 2009
    #3
  4. Misiu

    SG Guest

    On 17 Mrz., 10:52, SG <> wrote:
    > (I didn't test it)


    which explains the error:

    >   template<typename T>
    >   class test : public if_<
    >     same_<T,typename std::vector<T>::iterator>::value,


    The parameters to this meta function "same_" won't work like
    intended. You would need some other meta function which tests whether
    T is an iterator and if yes deduces the associated value_type
    (possibly via std::iterator_traits) and then checks whether
    std::vector<value_type>::iterator==T.

    But I think this is really overkill. You might be better served with
    another design approach. It might be a good idea to restrict the use
    of specializations (i.e. to traits classes).

    Cheers!
    SG
     
    SG, Mar 17, 2009
    #4
  5. Misiu

    Misiu Guest

    > Think about it. How can the compiler check whether your type
    > parameter matches std::vector<T>::iterator for some unknown T? In
    > case of std::vector this may seem trivial but std::vector is an
    > ordinary class template which could have looked like this:
    >
    > template <typename T>
    > class vector { typedef int iterator; };
    >
    > If you then explicitly instantiate a class via
    >
    > template test<int>;
    >
    > what kind of class should be instantiated? In this case
    > int==vector<T>::iterator for ALL possible types T. The problem here
    > is deducing T or in other words inverting the meta function that maps
    > type T -> type std::vector<T>::iterator. Also, this wouldn't be
    > really a specialization since std::vector<T>::iterator could be
    > basically anything you want it to be via specialization of
    > std::vector.


    Thanks for explanantion.
    Regards,
    Misiu
     
    Misiu, Mar 17, 2009
    #5
    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. Chandra Shekhar Kumar

    template partial specialization

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    17
    Views:
    3,061
    tom_usenet
    Jun 25, 2003
  2. Joseph Turian
    Replies:
    2
    Views:
    492
  3. toton
    Replies:
    1
    Views:
    630
  4. vj
    Replies:
    1
    Views:
    505
  5. Hizo
    Replies:
    17
    Views:
    715
    itaj sherman
    Mar 7, 2011
Loading...

Share This Page