#include <cstdlib>
#include <iostream>
/**
Test Class for learning template-metaprogramming
*/
using namespace std;
template<int N> //General case for a number
class ShowValues {
public:
static inline void show()
{
cout << "Start: " << N << endl;
ShowValues< (N > 1) ? (N - 1): 1>::show();
cout << "Finish: " << N << endl;
}
};
template<>
class ShowValues<1> { //specialized case for when N = 1
public:
static inline void show()
{
cout << "Reached 1" << endl;
}
};
struct MyStruct
{
public:
int x, y;
MyStruct(int first=0, int second=0): x(first), y(second){};
int getX(){return x;};
int getY(){return y;};
};
typedef class RestrictedTemplateClass
{
public:
template<MyStruct &B> //template that has a restricted parameter - must be a MyStruct reference or greater
static void displayParameters()
{
cout << B.getX() << ", " << B.getY() << endl;
};
}RTC;
int main(int argc, char *argv[])
{
const MyStruct *const mStruct = new MyStruct (1, 2);
// RTC::displayParameters<mStruct*>(); //apparantly this doesnt work?
// ShowValues<10>::show();
cin.get();
return 0;
};
Why is it that I cannot call th method in RTC::displayParameters() with the template-argument mStruct* when it is clearly a constant reference?
The errors I receive when I uncomment the code are--
... In function `int main(int, char**)':
60 ... `mStruct' cannot appear in a constant-expression
60 ... template argument 1 is invalid
60 ... no matching function for call to `RestrictedTemplateClass::displayParameters()'
... [Build Error] [testingTemplateRecursion.o] Error 1