typedeffing a partial specialization?

S

Shriramana Sharma

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> ;
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top