|
-
July 22nd, 2002, 11:42 AM
#1
Templates: why can't I do this?
Code:
#include <list>
template<class C, class T>
class MyContainer
{
private:
C<T> m_container;
};
class A
{
};
int main()
{
MyContainer<std::list, A*> container();
return 0;
}
It doesn't like the line "C<T> m_container;" and gives me the error "error C2059: syntax error : '<'".
Jeff
-
July 22nd, 2002, 12:33 PM
#2
std::list is not a type, it is a template. The following code works correctly.
Code:
#include <list>
template<typename C>
class MyContainer
{
private:
C m_container;
};
class A
{
};
int main()
{
MyContainer<std::list<A*> > container(); // Remember to put a space between the "> >"
return 0;
}
Regards,
Paul McKenzie
-
July 22nd, 2002, 12:40 PM
#3
Paul,
I was just about to post the same thing, with one additional typedef:
Code:
#include <list>
#include <set>
template<class C>
class MyContainer
{
public:
typedef C::value_type value_type;
private:
C m_container;
};
class A
{
};
int main()
{
MyContainer<std::list<A*> > myList();
MyContainer<std::set<A*> > mySet();
return 0;
}
With the typedef for value_type defined in the std:: container , it's possible to return items in the container.
Thanks for the reply. I've found a good solution. However, I'm still not clear on why my original post does not work.
Jeff
-
July 22nd, 2002, 12:53 PM
#4
The reason why it doesn't work is that a template argument is looking for a concrete type, for example, int, double, char, std::string, std::list<int>, std::list<A*>, etc.
std::list is not a type. It is a template class that defines a family of types. However, std::list<int> is a concrete type, since you are describing a list of integers.
And BTW, your int main() function now has declared two functions, not instances:
Code:
int main()
{
MyContainer<std::list<A*> > myList(); // Function returning a MyContainer<std::list<A*> >
MyContainer<std::set<A*> > mySet(); // Function returning a MyContainer<std::set<A*> >
MyContainer<std::list<A*> > myList2; // Instance of MyContainer<std::list<A*> >
MyContainer<std::set<A*> > mySet2; // Instance of MyContainer<std::set<A*> >
}
Regards,
Paul McKenzie
-
July 22nd, 2002, 12:57 PM
#5
It's starting to make sense. Thanks for the reply.
And yes, I noticed my function declarations. Well, I noticed them as soon as I tried to use them .
Jeff
-
July 22nd, 2002, 04:11 PM
#6
Doesn't ANSI C++ allow
Code:
template<template <class> class C, class T>
class MyContainer
{
// blah, blah
C<T> m_container;
};
int main()
{
MyContainer<std::list, int> mc;
}
Of course, VC++ doesn't support that syntax, but who's surprised by that?
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 22nd, 2002, 04:22 PM
#7
Can somebody try this on a better compiler?
Jeff
-
July 23rd, 2002, 03:31 AM
#8
This compiles with Comeau:
Code:
#include <list>
template<template <class, class> class C, class T>
class MyContainer
{
// blah, blah
C<T, std::allocator<T> > m_container;
};
int main()
{
MyContainer<std::list, int> mc;
}
Note that I had to change it a bit because list actually has two template parameters.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 23rd, 2002, 05:26 AM
#9
Thanks Graham,
I knew that there are now template-template parameters, but didn't go to Comeau to try out the new syntax.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|