One template question

Started by
5 comments, last by nlbs 15 years, 4 months ago
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.
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?'

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

You probably wanted:
template <typename T, typename CONT = std::vector<T> > struct Stack{    CONT elem;};
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
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.
Try:
template <typename T, typename CONT = std::vector<T> > struct Stack{    CONT elem;};

EDIT: Waaaaay too slow [smile]
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]
Thanks all understood
I forgot that std::vector does not name a type rather std::vector<SOMETYPE> is a Typename

This topic is closed to new replies.

Advertisement