• Advertisement
Sign in to follow this  

One template question

This topic is 3365 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
Advertisement
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
You probably wanted:

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

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
Try:

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



EDIT: Waaaaay too slow [smile]

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
Thanks all understood
I forgot that std::vector does not name a type rather std::vector<SOMETYPE> is a Typename

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement