What is the difference between
template <class T>
class why
{
public:
why()
{}
};
and
template <class T>
class why
{
public:
why<T>()
{}
};
They both seem to work the same and compile.
EDIT
And, if they are truly the same, why is this functionality even in the C++ language?
They’re equivalent. In the former,
whyis an injected name that’s defined to be the same aswhy<T>(whereTis the actual template argument.) This exists for ease of typing.Note that this injection is into the class’s scope, and not outside of it. Obvious, but given:
A common mistake might be to try and define
xlike this:But this would be an error, as
fooin the return type needs template arguments. Thefooin the parameter list is okay, though, because afterfoo<T>::we’re in the class’s scope, andfoois defined to befoo<T>. So the correct way to do this is either of these: