Sign in to follow this  

One template question

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

template <typename T, typename CONT /*= std::vector*/ > 
struct Stack{
    CONT elem;
};
The above code compiles fine as it should be If I just uncomment the std::vector one it fires compilation errors saying
error: expected type-specifier
error: expected '>'
But I don't find any syntactic or symantic error in the above code. can anybody please point it out.

Share this post


Link to post
Share on other sites
It's because std::vector is a templated type too, so you need to specify something for it to template on. For example, template <typename T, typename CONT = std::vector<int> > will work because now the compiler knows 'a vector of what?'

Share this post


Link to post
Share on other sites
For std::vector to be considered a complete type it needs its one parameter specified. You might want something like this


template <typename T, typename CONT = std::vector<T> >
struct Stack{
CONT elem;
};




And as they say... ninja'd... twice :p

Share this post


Link to post
Share on other sites
You can do what you want by using template template parameters:

#include <vector>

template<typename T, template<typename T, typename Alloc> class CONT = std::vector>
struct Stack{
CONT<T, std::allocator<T> > elem;
};


int main()
{
Stack<int, std::vector> s;
return 0;
}


[Edited by - D_Tr on December 5, 2008 10:26:07 AM]

Share this post


Link to post
Share on other sites

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this