Archived

This topic is now archived and is closed to further replies.

Amnesty

Various template questions

Recommended Posts

1) making a ctor-like function a member template a bad idea? Recently i tried to make a member template ctor that accepted a generation functor (which was all policy-ed up so i can easily create new generations types). Worked pretty good and i liked what i saw. With just a few policys i could easily make any game-type object. My world ended when i tried to write the default ctor and copy constructor. Any way to get it to all work? 2) Can someone please explain this strange template syntax for me
template
<
typename T,
template <class> class OwnershipPolicy = RefCounted
>
class widget;
I really don''t understand that nested template line. Or how to use it in my own code. I assume it lets the caller paramertize OwnershipPolicy at the call site? 3) What does typename do and how do you use it? This has bugged me for a long time. I know it does something more than just beign interchangeable with class in template declartions. Thanks a bunch -_-

Share this post


Link to post
Share on other sites
quote:
1) making a ctor-like function a member template a bad idea?

What error messages do you get? Show us some code.
quote:
2) Can someone please explain this strange template syntax for me

That is a template template parameter, with the default type set to RefCounted. RefCounted is a template class and the template template parameter allows Widget to specify the type that RefCounted should use:

Widget<int, RefCounted> widget_; // create a widget

.
.
.
template <typename T>
struct RefCounted
{
};

template <typename T, template <class> class RefCounted>
struct Widget
{
RefCounted<T> refc_; // Widget creates a RefCounted with type T

};

Without a template template parameter, one would have to declare Widget<int, RefCounted<int> >, which is ugly and bad, especially if RefCounted and Widget must have the same type T.
quote:3) What does typename do and how do you use it?
Sometimes you need to explicitly tell the compiler to treat something as a type:
   
struct A
{
typedef int INT;
};

template <typename T>
struct B
{
typename T::INT* b;
};

Without typename , the compiler wouldn't know if it should multiply T::INT by b or if it's a pointer.

Edit: < >

[edited by - Matsen on October 5, 2003 6:58:05 AM]

Share this post


Link to post
Share on other sites