but I don't quite understand why you would default it to void?
You can create a void* variable, but not a void variable, so what's going on?

03-24-2009

EVOEx

What about:

Code:

class TemplateClass {
T *test;
};

03-24-2009

cpjust

The only thing I could imagine is when T is declared as a variable like that, but then why use void* if you're using a template? Aren't void* pointers more useful in C than in C++?

03-24-2009

brewbuck

Quote:

Originally Posted by cpjust

I've seen this in a lot of example code:

Code:

template <typename T=void>

but I don't quite understand why you would default it to void?
You can create a void* variable, but not a void variable, so what's going on?

Sometimes these things are used to deliberately cause compile errors in certain situations. For instance, if you want to restrict the types of template parameters which are allowed to be used. See boost::enable_if

03-25-2009

EVOEx

Hmm brewbuck may have a point. But I'd rather do that something like this:

Code:

template<typename T> class SomeClass;

template<>
class SomeClass<AllowedType1> {
};

Rather than defaulting to void and declaring a variable that wouldn't be allowed.

But, true, void pointers can often be avoided in C++. So the system you read it at would probably be badly designed anyway.

03-25-2009

cpjust

Quote:

Originally Posted by EVOEx

But, true, void pointers can often be avoided in C++. So the system you read it at would probably be badly designed anyway.