Skip to main content
GameDev.net gamedev.net
🔒 Locked

One template question

Started by nlbs Dec 5, 2008 at 10:17 AM 5 replies 850+ views
Original Post
nlbs
nlbs
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.
PKLoki
PKLoki
It's because std::vector is a templated type too, so you need to specify something for it to template on. For example, template > will work because now the compiler knows 'a vector of what?'
SiCrane
SiCrane
You probably wanted:
template <typename T, typename CONT = std::vector<T> > struct Stack{    CONT elem;};
Guthur
Guthur
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.
Evil Steve
Evil Steve
Try:
template <typename T, typename CONT = std::vector<T> > struct Stack{    CONT elem;};

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.