typedeffing a partial specialization?

Discussion in 'C++' started by Shriramana Sharma, Jun 27, 2013.

  1. Hi -- in case of templates that take more than one parameter, I wondered if it would be possible to partially specialize it (if that's the correct technical term) and typedef it to something else, for example:

    # include <iostream>
    # include <vector>

    template < typename T, unsigned int S >
    struct tuple {
    tuple () : vec(S) {}
    T& operator [] ( unsigned int i ) { return vec.at(i) ; }
    private:
    std::vector<T> vec ;
    } ;

    template < typename T >
    typedef tuple<T,2> pair<T> ;

    However, the compiler complains saying:

    typedef-template.cpp:13:20: error: no template named 'pair'; did you mean 'std::pair'?

    Apparently the following is one way to do it:

    template < typename T >
    struct pair : public tuple<T,2> {} ;

    However I wonder, if I am able to do:
    typedef tuple<double,2> twoDoubles ;

    why can't I do:

    template < typename T >
    typedef tuple<T,2> pair<T> ;
    Shriramana Sharma, Jun 27, 2013
    #1
    1. Advertising

  2. On 6/27/2013 11:35 AM, Shriramana Sharma wrote:
    > Hi -- in case of templates that take more than one parameter, I

    wondered if it would be possible to partially specialize it (if that's
    the correct technical term) and typedef it to something else, for example:
    > [...]


    Look up "alias templates". I think you want

    # include <vector>

    template < typename T, unsigned int S >
    struct tuple {
    tuple () : vec(S) {}
    T& operator [] ( unsigned int i ) { return vec.at(i) ; }
    private:
    std::vector<T> vec ;
    };

    template<class T> using pair = tuple<T,2>;

    You need a compliant C++11 compiler for that, too.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 27, 2013
    #2
    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,011
    tom_usenet
    Jun 25, 2003
  2. Philip Lawatsch

    Partial Specialization workaround

    Philip Lawatsch, Jul 16, 2003, in forum: C++
    Replies:
    9
    Views:
    384
    Philip Lawatsch
    Jul 16, 2003
  3. Alexander Stippler

    partial specialization of function template

    Alexander Stippler, Aug 15, 2003, in forum: C++
    Replies:
    1
    Views:
    536
    Rob Williscroft
    Aug 15, 2003
  4. Joseph Turian
    Replies:
    2
    Views:
    451
  5. Johannes Schaub (litb)

    Typedeffing "auto"

    Johannes Schaub (litb), Oct 13, 2010, in forum: C++
    Replies:
    2
    Views:
    208
    Joe Gottman
    Oct 14, 2010
Loading...

Share This Page