In that test code:
#include <string>
#include <iostream>
using namespace std;
template <typename T> class Signal;
template <typename T, typename U>
class Signal<T (U)>
{
public:
Signal<T (U)>(T (*ptr)(U))
{
}
};
void Print(string const& str)
{
cout << str << endl;
}
int main(int argc, char *argv[])
{
Signal<void (string const&)> sig = &Print;
return 0;
}
Why do I have to write template <typename T> class Signal;?
Why do I have to specify it ?
You’re creating a specialization of
Signalthat combines the arbitrary typesTandUinto the formT(U). This is put together as the specializationSignal<T(U)>: only one type is in the parameter, which why we forward-declared theSignaltaking only one typeT. This wouldn’t be possible without that declaration.Here’s a simple example:
The types
voidandintare bound to the typesTandUrespectively. This is combined into the typevoid(int)used in the primary declaration ofAto specialize the class.